Use a .Jar Java Library API in C#

Use a .jar java library API in C#?

You can do it using IVKM.Net. IVKM.NET includes an application called ikvmc. Here’s the documentation for this tool:

http://www.ikvm.net/userguide/ikvmc.html

To use it compile your java code into a Jar.

Then run the ikvmc program:

ikvmc myCode.jar

If your jar contains a main() function, it will be converted into an exe that can be run on the CLR. Otherwise it will be converted into dll’s. To use the dll’s in your project just add them as references in Visual Studio and the same API that was available to you in Java will be accessible in your .Net application.

You can also include the IKVM.GNU.Classpath.dll dll from ikvmc and use the standard java class libraries in your application.

Can I add JAR file as library in C# project

I was able to use JAR file in C# after converting it into DLL using ikvm. Here's the steps I followed:

  1. Download the latest version of iKVM from https://sourceforge.net/projects/ikvm/ & Unzip the zip file.
  2. Open command prompt in windows and run this command:
    ikvmc.exe -out:.dll .jar
  3. Above steps should generate the DLL which you can reference in C# project.

There is also another way using JNI4NET - https://github.com/jni4net/jni4net/ but I haven't explored it much as I was able to achieve what I want using iKVM

Happy Coding!

Thanks

Adding jar file to C# class library

You can add the jar file to your project and in the file properties set it to content and Copy if newer.

This will copy it to the output folder during compilation and ensure it is distributed with the application.

An alternative is to embed the file as a resource - this makes it part of the application executable and let you access it within the application. In order to execute it separately, however, you will need to extract it and save to a file.

How to run jar file form C# code

You will have to provide -jar switch to java command.

For example, the command to execute JAR file is,

java -jar D:\\DATA\\PROJECT\\LicensingManagement\\Assignment\\JavaLogin.jar

So you may try,

myProcess.StartInfo.Arguments = "-jar D:\\DATA\\PROJECT\\LicensingManagement\\Assignment\\JavaLogin.jar";

Call Java Method from API in .NET

Here you go :) I've used it myself and was very please with the implementation.

IKVM: Using Java API's in .NET Applications

  • (1) If you just want some libraries
    from Java.

  • (2.1) If you have access to
    the code.

  • (2.2) Last resort,
    dynamically load the Java into .Net
    (interpreter)

c# - How to execute a .Jar file with Arguments.Length 8191

You could make your program receive a file as an argument and read it.
The file would have one or more lines depending the number of arguments you'd like to pass to the program.

Your program would need to receive the file and parse it line by line.



Related Topics



Leave a reply



Submit