Click here to Skip to main content
11,713,310 members (82,227 online)
Click here to Skip to main content

How To Use Java Packages In .NET

, 17 May 2013 CPOL 9.6K 18
Rate this:
Please Sign up or sign in to vote.
This tip is about how to use Java packages in .NET using IKVM.NET

Introduction

In this tip, I am going to describe how to use Java Packages in .NET using IKVM.NET. In my previous tip, I have briefly described all about IKVM.NET. So before reading this tip, you can refer to that one. IKVM.NET provides this facility with the help of OpenJDK Project.

Using the Code

So for using IKVM.NET library, first of all you have to download it from here. After downloading IKVM.NET , unzip the file. You can see the following folders and files inside unzip file.

Now suppose you are creating a project in C#.NET and you want to use Java packages in this project, then we need to add reference of the package which is required. For each package, there is a separate DLL file so you can add any package.

As you can see in the above image, there are lots of packages present as DLL file so you can add references of the required packages.

Here, I am going to use GZIP Compression class present in Java.util.zip package of Java. I know all of you have a question now, why I am using GZIP compression of Java while it presents in .NET already. So the reason behind that is GZIP compression provided by Java is better than .NET.

Let's see how to use it. So here we need to add IKVM.OpenJDK.Util.dll and IKVM.OpenJDK.Code.dll because GZIPOutputStream class is present in IKVM.OpenJDK.Util package and FileInputStream class which is required for file manipulation is present in Java.io that resides in IKVM.OpenJDK.Code package.

using java.io;
using java.util.zip;
namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            string srcfile="c:\\source.txt";
            string dstfile="c:\\destination.txt";

            FileInputStream fin = new FileInputStream(srcfile);
            GZIPOutputStream fout = new GZIPOutputStream(new FileOutputStream(dstfile));
            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = fin.read(buffer)) != -1) //srcfile.getBytes()
            {

                System.Console.WriteLine(bytesRead);
                fout.write(buffer, 0, bytesRead);
            }

            fin.close();
            fout.close();

            //CompressStringToFile(dstfile, srcfile);
        }      
    }
}  

So as you can see in the above code by using IKVM.NET, we can use any Java package very easily without any overhead. You can use the same code that you have used in Java.

Points of Interest

You can use many powerful facilities that are provided by Java packages, without any overhead. In this tip, I am using a very simple utility of Java but if you want, you can use it accordingly and find it more useful later on.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhishek Kumar Goswami
Software Developer
India India
I like to code and I really enjoy to share my knowledge with all, Its my passion.

http://abhishekblog.com/

You may also be interested in...

Comments and Discussions

 
Bugbroken link Pin
Bib3477020-May-13 5:30
memberBib3477020-May-13 5:30 
AnswerRe: broken link Pin
abhishek_jmk20-May-13 6:43
memberabhishek_jmk20-May-13 6:43 
GeneralRe: broken link Pin
abhishek_jmk21-May-13 21:32
memberabhishek_jmk21-May-13 21:32 
GeneralMy vote of 3 Pin
ibrahim_ragab18-May-13 1:13
memberibrahim_ragab18-May-13 1:13 
GeneralMy vote of 3 Pin
Mohit_Rudra17-May-13 18:06
memberMohit_Rudra17-May-13 18:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 17 May 2013
Article Copyright 2013 by Abhishek Kumar Goswami
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid