• home
  • forum
  • my
  • kt
  • download
  • Using a Java class in .Net

    Author: 2008-08-04 12:04:40 From:

    It might sound a little complex using Java package in .Net but trust me, the interoperability is quite easy ;) I’ll explain the procedure in few simple steps:

    1. Create a simple Java class, for e.g.

    public class CalculatorX {

    public int Add(int firstNum, int secondNum)
    {
    return firstNum + secondNum;
    }

    }

    2. Compile it to create a .class

    > javac calculatorx.java

    3. In case you have more than one class, you may package it into a JAR file.

    > jar cvf calculatorx.jar *.class

    4. Now, run *JBImp. JBImp is a Microsoft Java-language bytecode to MSIL converter.

    > jbimp /t:library calculatorx.class

    (or calculatorx.jar if you have created a package)

    You’ll get the following output:

    “Microsoft (R) Java-language bytecode to MSIL converter version 2.0.50727.42
    for Microsoft (R) .NET Framework version 2.0.50727
    Copyright (C) Microsoft Corp 2000-2002. All rights reserved.

    Created CalculatorX.dll”

    6. Here you go! Your .Net assembly named CalculatorX.dll is ready :D
    7. Next, create a .Net project. Add a reference to the newly created .dll file and try it out!

    (See the sample code below)

    static void Main(string[] args)
    {
    CalculatorX c1 = new CalculatorX();
    Console.Write(c1.Add(1, 2).ToString());
    }

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      NET (134)

    New

    Hot