using java classes in your

Upload: ney-galecio

Post on 02-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Using Java Classes in Your

    1/6

  • 8/10/2019 Using Java Classes in Your

    2/6

    19/6/2014 Using Java Classes in your .NET Application - CodeProject

    http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application 2/6

    Jeff Prosise on .NET

    Simple Source Line Counter in

    Java for Java

    Introduction to Visual J# .NET

    The Common LanguageRuntime (CLR) and JavaRuntime Environment (JRE)

    SharpHSQL - An SQL enginewritten in C#

    .NET and J2EE interoperabilityfor .Net Developers

    Related Research

    Enterprise Imaging on the Web:A How To Guide for Developers

    In-The-Wild Testing: How toEnsure Your Apps Work in the

    Real World

    Developer Tips for Scanning onthe Web

    Essential Keys to MobileUsability

    directory. Open a command or shell window, cd to C:\ikvm\bin, and type ikvm.

    If your system is operating correctly, you should see the following output:

    usage: ikvm [-options] [args...] (to execute a class) or ikvm -jar [-options] [args...]

    (to execute a jar file)

    For Linux based systems, the setup is similar as above. This is all you need to do for running the

    demo application.

    Our Demo Java Business Class (JavaToNet.java)

    Collapse | Copy Code

    publicclassJavaToNet{ publicstaticvoidmain(String[] args)

    { System.out.println("This is a demonstration Program which\n"); System.out.println("shows the conversion of Java class to\n"); System.out.println("a .NET dll\n"); } public staticdoubleAddNumbers(doublea,doubleb){ doublec = 0; c = a + b; returnc;

    } public staticdoubleSubNumbers(doublea,doubleb){ doublec = 0;

    c = a - b; returnc;

    } public staticdoubleMulNumbers(doublea,doubleb){ doublec = 0; c = a * b; returnc;

    } public staticdoubleDivNumbers(doublea,doubleb){ doublec = 0; c = a / b; returnc;

    }

    }

    Our Java class is very simple. It has four functions for add, subtract, multiply and divide that take

    two double values and return a result. Our objective is to access these functions through our C#

    application. Compile the above Java file to get the JavaToNet.class . We will use this Java class fileto generate the .NET DLL to be referenced in our C# program.

    Using IKVM.NET to Convert Java Class to

    .NET DLL

    Copy the above Java class file (JavaToNet.class ) to the C:\ikvm\bindirectory. Now run the

    following command:

    This would create the JavaToNet.dll from the JavaToNet.class file. There are other command line

    option for ikvmc.exe. For example: ikvmc target:exe javaToNet.class would create

    an EXE and not a DLL. You can get all the options by typing ikvmc in the command line.

    Setting Up Your .NET Development

    Environment

    1. Start by creating a C# Windows application project.

    2. Drag and drop controls into the form as shown:

    http://www.codeproject.com/ResearchLibrary/46/Essential-Keys-to-Mobile-Usabilityhttp://www.codeproject.com/ResearchLibrary/57/Developer-Tips-for-Scanning-on-the-Webhttp://www.codeproject.com/ResearchLibrary/18/In-The-Wild-Testing-How-to-Ensure-Your-Apps-Work-ihttp://www.codeproject.com/ResearchLibrary/56/Enterprise-Imaging-on-the-Web-A-How-To-Guide-for-Dhttp://www.codeproject.com/Articles/22560/NET-and-J-EE-interoperability-for-Net-Developershttp://www.codeproject.com/Articles/1136/SharpHSQL-An-SQL-engine-written-in-Chttp://www.codeproject.com/Articles/1825/The-Common-Language-Runtime-CLR-and-Java-Runtime-Ehttp://www.codeproject.com/Articles/1440/Introduction-to-Visual-J-NEThttp://www.codeproject.com/Tips/139036/Simple-Source-Line-Counter-in-Java-for-Javahttp://www.codeproject.com/Articles/688/Jeff-Prosise-on-NET
  • 8/10/2019 Using Java Classes in Your

    3/6

    19/6/2014 Using Java Classes in your .NET Application - CodeProject

    http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application 3/6

    3. Add the following DLLs as references to the project. Both DLLs are present in the

    C:\ikvm\binfolder.

    JavaToNet.dll

    IKVM.GNU.Classpath.dll

    4. Add the following code to the button click event of the Calculate button:

    Collapse | Copy Code

    privatevoidbtnCal_Click(objectsender, System.EventArgs e){if(rdAdd.Checked == true) { txtResult.Text = Convert.ToString(JavaToNet.AddNumbers (Convert.ToDouble(txtNum1.Text),Convert.ToDouble(txtNum2.Text))); }elseif(rdSub.Checked ==true) { txtResult.Text = Convert.ToString(JavaToNet.SubNumbers (Convert.ToDouble(txtNum1.Text),Convert.ToDouble(txtNum2.Text)));

    } elseif(rdMul.Checked == true) { txtResult.Text = Convert.ToString(JavaToNet.MulNumbers (Convert.ToDouble(txtNum1.Text),Convert.ToDouble(txtNum2.Text)));

    } else

    { txtResult.Text = Convert.ToString(JavaToNet.DivNumbers (Convert.ToDouble(txtNum1.Text),Convert.ToDouble(txtNum2.Text)));

    }}

    5. Add the following usingdirective on the top of the *.csfile:

    Collapse | Copy Code

    usingTimeZone = java.util.TimeZone;

    6. Add the following code to the button click event of the Time Zone button.

    Collapse | Copy Code

    privatevoidbtnTimeZone_Click(objectsender, System.EventArgs e){MessageBox.Show(TimeZone.getDefault().getDisplayName());}

    7. Compile and run the application. The C# application would now call the AddNumbers(),

    SubNumbers(), MulNumbers() and DivNumbers() functions present in the

    JavaToNet.dlland return the result.

    8. Click on the Time Zone button. The application accesses the java.util.TimeZone class

    and displays the exact time zone of the place.

  • 8/10/2019 Using Java Classes in Your

    4/6

    19/6/2014 Using Java Classes in your .NET Application - CodeProject

    http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application 4/6

    ChayanWeb Developer

    India

    Chayan Ray has been working as a Technical Consultant in a CMM level 5 company in India. His

    technical domain includes ASP.NET, C#, PHP, Perl, Cold Fusion, MySQL and MSSQL 2000.

    Conclusion

    Since these methods had originally been written in Java, IKVM.NET provides us an easy and viable

    way to access those classes and methods from a .NET application. Similarly as shown above in the

    Time Zone example, you can access most of the existing Java packages (e.g. java.io , java.util , etc.)

    and use them in your application.

    However there are certain drawbacks. IKVM.NET, while still being actively developed, has limited

    support for AWT classes and hence porting Java GUI can be ruled out at present. Also some of

    the default Java classes are still being ported so you might not get all the functionalities you

    require. Also if your application depends on the exact Java class loading semantics, you might

    have to modify it to suit your needs.

    History

    03-25-06 Initial publication

    License

    This article, along with any associated source code and files, is licensed under The Code Project

    Open License (CPOL)

    About the Author

    Article Top

    http://-/?-http://www.codeproject.com/info/cpol10.aspxhttp://www.ucancode.net/index.htmhttp://www.codeproject.com/Members/User-248385
  • 8/10/2019 Using Java Classes in Your

    5/6

    19/6/2014 Using Java Classes in your .NET Application - CodeProject

    http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application 5/6

    Add a Comment or QuestionSearch this forum Go

    Comments and Discussions

    Profile popups Spacing Relaxed Noise Medium Layout Normal Per page 25

    Update

    First Prev Next

    Shailenderrajput 3-Feb-14 3:36

    MrKyaw 21-Jan-13 2:24

    sonam5021732-Jan-13 8:24

    sonam502173 2-Jan-13 8:23

    sonam502173 2-Jan-13 8:22

    Member 4559750 16-Jun-12 8:37

    NarVish 25-May-12 8:29

    rajendranmca2008 25-Oct-11 2:42

    sanya.fesak 22-Sep-11 0:12

    majed.idrees 16-Apr-11 21:20

    Nguyen_Chuong 21-Feb-11 9:54

    dummy programmer24 1-Dec-10 20:13

    Chayan 2-Dec-10 3:08

    dummy programmer24 2-Dec-10 9:11

    DilushaM 9-Jul-10 14:04

    IthilienX 26-Oct-08 19:14

    Rini Jackson 14-Mar-12 10:20

    Nitin Sawant 30-Nov-12 5:40

    MHSrinivasan 19-Jun-08 5:15

    Askalo 6-Jul-07 10:37

    Yeast27 25-Sep-06 8:04

    devesh.kumar 7-Jul-06 4:49

    Vitaly Shelest 22-Jun-06 8:02

    Missing file

    Error Message ->IKVMC0002: Output File

    is "JavaToNet.dll"

    Getting errors while converting Jar to dll

    using IKVM - please help!

    Getting Class not found errors while

    converting jar to dll using IKVM - please

    help!!!!

    Getting Class not found errors while

    converting jar to dll using IKVM

    ikmv class java to .net c#

    How to use IKVM without creating dll

    Thank you...

    Thanks

    IKVM Framework is Updated

    the architecture in IKVM ?

    Jar to Native Dll

    Re: Jar to Native Dll

    Re: Jar to Native Dll

    My vote of 5

    using a jar file with dependent jars

    Re: using a jar file with dependent jars

    Re: using a jar file with dependent jars

    Has Dependent Jar files?

    Problem with IKVM

    Does it only work with static method

    get a error that "unsupported class file

    version:"

    IKVM.NET violates Java and .NET

    standards!

    http://www.codeproject.com/Messages/1543918/IKVM-NET-violates-Java-and-NET-standards.aspxhttp://www.codeproject.com/Messages/1565636/get-a-error-that-unsupported-class-file-version.aspxhttp://www.codeproject.com/Messages/1683483/Does-it-only-work-with-static-method.aspxhttp://www.codeproject.com/Messages/2117354/Problem-with-IKVM.aspxhttp://www.codeproject.com/Messages/2603964/Has-Dependent-Jar-files.aspxhttp://www.codeproject.com/Messages/4440525/Re-using-a-jar-file-with-dependent-jars.aspxhttp://www.codeproject.com/Messages/4190250/Re-using-a-jar-file-with-dependent-jars.aspxhttp://www.codeproject.com/Messages/2781359/using-a-jar-file-with-dependent-jars.aspxhttp://www.codeproject.com/Messages/3530273/My-vote-of.aspxhttp://www.codeproject.com/Messages/3684738/Re-Jar-to-Native-Dll.aspxhttp://www.codeproject.com/Messages/3684059/Re-Jar-to-Native-Dll.aspxhttp://www.codeproject.com/Messages/3683808/Jar-to-Native-Dll.aspxhttp://www.codeproject.com/Messages/3778342/the-architecture-in-IKVM.aspxhttp://www.codeproject.com/Messages/3859236/IKVM-Framework-is-Updated.aspxhttp://www.codeproject.com/Messages/4031118/Thanks.aspxhttp://www.codeproject.com/Messages/4060917/Thank-you.aspxhttp://www.codeproject.com/Messages/4262308/How-to-use-IKVM-without-creating-dll.aspxhttp://www.codeproject.com/Messages/4283035/ikmv-class-java-to-net-csharp.aspxhttp://www.codeproject.com/Messages/4463167/Getting-Class-not-found-errors-while-converting-ja.aspxhttp://www.codeproject.com/Messages/4463168/Getting-Class-not-found-errors-while-converting-ja.aspxhttp://www.codeproject.com/Messages/4463170/Getting-errors-while-converting-Jar-to-dll-using-I.aspxhttp://www.codeproject.com/Messages/4476576/Error-Message-IKVMC-Output-File-is-JavaToNet-dll.aspxhttp://www.codeproject.com/Messages/4750117/Missing-file.aspxhttp://www.codeproject.com/script/Membership/View.aspx?mid=2648168http://www.codeproject.com/script/Membership/View.aspx?mid=3169492http://www.codeproject.com/script/Membership/View.aspx?mid=1862169http://www.codeproject.com/script/Membership/View.aspx?mid=3066546http://www.codeproject.com/script/Membership/View.aspx?mid=877304http://www.codeproject.com/script/Membership/View.aspx?mid=4187012http://www.codeproject.com/script/Membership/View.aspx?mid=7048993http://www.codeproject.com/script/Membership/View.aspx?mid=5317483http://www.codeproject.com/script/Membership/View.aspx?mid=7292947http://www.codeproject.com/script/Membership/View.aspx?mid=7597442http://www.codeproject.com/script/Membership/View.aspx?mid=248608http://www.codeproject.com/script/Membership/View.aspx?mid=7597442http://www.codeproject.com/script/Membership/View.aspx?mid=7692279http://www.codeproject.com/script/Membership/View.aspx?mid=1032729http://www.codeproject.com/script/Membership/View.aspx?mid=6547884http://www.codeproject.com/script/Membership/View.aspx?mid=5497773http://www.codeproject.com/script/Membership/View.aspx?mid=4261880http://www.codeproject.com/script/Membership/View.aspx?mid=4559750http://www.codeproject.com/script/Membership/View.aspx?mid=9658973http://www.codeproject.com/script/Membership/View.aspx?mid=9658973http://www.codeproject.com/script/Membership/View.aspx?mid=9658973http://www.codeproject.com/script/Membership/View.aspx?mid=3503712http://www.codeproject.com/script/Membership/View.aspx?mid=10567702http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application?fid=283780&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Normal&spc=Relaxed&fr=26#xx0xxhttp://www.codeproject.com/KB/FAQs/MessageBoardsFAQ.aspxhttp://www.codeproject.com/script/Forums/Edit.aspx?fid=283780&floc=/Articles/13549/Using-Java-Classes-in-your-NET-Application
  • 8/10/2019 Using Java Classes in Your

    6/6

    19/6/2014 Using Java Classes in your .NET Application - CodeProject

    http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application 6/6

    Permalink| Advertise | Privacy| MobileWeb02 | 2.8.140618.1 | Last Upd ated 24 Mar 2006

    Article Copyright 2006 by ChayanEverything else Copyright CodeProject, 1999-2014

    Terms of Service

    Layout: fixed| fluid

    surjithpk 12-Apr-06 7:13

    Chayan 12-Apr-06 10:12

    Last Visit: 11-Jun-14 12:11 Last Update: 19-Jun-14 5:52 Refresh 1 2 Next

    General News Suggestion Question Bug Answer Joke Rant Admin

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

    Help regarding IKVMC tool

    Re: Help regarding IKVMC tool

    http://www.codeproject.com/Messages/1446971/Re-Help-regarding-IKVMC-tool.aspxhttp://www.codeproject.com/Messages/1446640/Help-regarding-IKVMC-tool.aspxhttp://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application?fid=283780&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Normal&spc=Relaxed&fr=26#xx0xxhttp://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application?fid=283780&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Normal&spc=Relaxed&fr=26#xx0xxhttp://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application?fid=283780&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Normal&spc=Relaxedhttp://www.codeproject.com/script/Membership/View.aspx?mid=248608http://www.codeproject.com/script/Membership/View.aspx?mid=1662609http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application?PageFlow=Fluidhttp://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application?PageFlow=FixedWidthhttp://www.codeproject.com/info/TermsOfUse.aspxmailto:[email protected]://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application?display=Mobilehttp://www.codeproject.com/info/privacy.aspxhttp://developermedia.com/http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application