Convert JAR file to DLL for use in Visual Studio
It is possible to convert a Java Jar file to .Net Dynamic-link library(DLL) file and use it in Visual Studio Projects. IKVM.Net an implementation of Java for Mono and the Microsoft .Net Framework makes this possible.
Software Requirements
- IKVM.Net Library
- Java JDK 7
- Microsoft .Net Framework 3.5 or above
JAR to DLL Conversion limitations when using IKVM.Net Library
The JAR Files which is to be converted to DLL should be complied with JAVA JDK 1.7 or below. This limitation is because IKVM currently does not support converting JAR files compiled with JDK 1.8 or above.
JAR To DLL Conversion Command
ikvmc -target:library -out:<<outputFileName>>.dll -recurse:"<<jarFilesPath>>/*.jar"
The above command will convert all the JAR Files present in the folder path specified in the -recurse argument to a DLL file in the IKVM bin folder with the name specidied in -out argument.
Example
ikvmc -target:library -out:CraftedForEveryoneJarToDLL.dll -recurse:"./*.jar"
Step by Step Example to Convert a Java Project Exported as JAR to DLL and use in Visual Studio Projects.
Step 1 - Create a new Java Project in Eclipse
- Click File -> New -> Project... -> Java -> Java Project
- Enter the project name in the "Project Name:" text box
- Set the "Use an execution environment JRE" to JavaSE-1.7 or below
- Click Finish
The Demo project is created as "CraftedForEveryoneJarToDll"
Step 2 - Create a new Package
- Right Click "src" folder -> New -> Package
- Enter the package name in "New Java Package" window which is opened
- Click Finish
The Demo Package is created as "craftedforeveryone.com"
Step 3 - Create required Classes
- Right Click newly created package -> New -> Class
- Enter the Class name and click Finish
Here the class "Learning.java" with a sample code is created for demo purpose
package craftedforeveryone.com; public class Learning { public static String WelcomeMessage; public String userName; public Learning() { userName="Default User"; WelcomeMessage="Hello "+userName+" !"; } public Learning(String userName) { this.userName=userName; WelcomeMessage="Hello "+userName+" !"; } public String getUserName() { return userName; } }
Step 4 - Export the Project as JAR File
- Right Click the Project Folder -> Export
- Select Java -> JAR File and click Next
- Choose the destination file name using Browse Button
- Click Finish
Step 5 - Convert JAR File to DLL
- Download and extract the IKVM.Net Library from https://www.ikvm.net/download.html
- Copy the Exported JAR File to IKVM bin folder
- Open the command prompt and navigate to IKVM bin folder
- Execute the command ikvmc -target:library -out:CraftedForEveryoneJarToDLL.dll -recurse:"./*.jar"
- All the JAR files in the IKVM bin folder will be converted to a single DLL file CraftedForEveryoneJarToDLL.dll
CraftedForEveryoneJarToDLL.dll is used only for demo purpose it can be replaced with your file name.
Step 6 - Reference the JAR file converted to DLL in Visual Studio Project
- Create a new Visual Studio project or Open an existing project
- Right click References and click Add Reference...
- Browse to IKVM Bin folder and in the Reference Manager window
- Add CraftedForEveryoneJarToDLL.dll and IKVM.Open.JDK.Core.dll as references
- Click Ok
Step 7 - Execute the functions from the JAR file converted to DLL
- Use the using keyword to import the packages from the JAR to Dll file
- Write the suitable code as per your needs with objects of the class from JAR file
An example code is given below
using System;
using System.Windows.Forms;
using craftedforeveryone.com;
namespace ConvertJARtoDLL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Learning learning=new Learning("CraftedForEveryone.com");
String userName = learning.getUserName();
MessageBox.Show(Learning.WelcomeMessage,userName);
}
}
}
Execute the code to see the output. Now you have successfully converted your Java JAR File and used it with C# Project in Visual Studio.
why do I get this error
warning IKVMC0111: Emitted java.lang.NoClassDefFoundError in “……
Hi Denis,
Please check if your Java Project Version is set to 1.7 or below.
If your project Java Version is 1.8 or above you will get this warning.
I keep getting this error:
fatal error IKVMC5015: Invalid path:
even though I have copied the .jar file into the ikvmbin\bin directory…
Hi Peter,
Could you please change the directory path to ikvmc folder before executing the command. That should resolve the issue.
I have dependency jars in web-inf/lib. I am getting the below warnings while converting to dll. And .net code is failing with noclassdeffounderror BouncyCastelProvider.
I am using jdk1.7.0_97 and ikvm8.1.5717. Could you please help.
warning IKVMC0100: Class “org.bouncycastle.jce.provider.BouncyCastleProvider” not found
I an copying the jar files out side bin and in ikvmc folder and still seeing the error : fatal error IKVMC5015: Invalid path: Could you please suggest
I’m Using ikvm-7.2.4630.5, the jar file is created in JavaSE-1.7 environment. this command “ikvmc -target:library -out:CraftedForEveryoneJarToDLL.dll -recurse:”./*.jar” runs without error. dll file is created. Dll and IKVM.Open.JDK.Core.dll is added to a C# console project (VS 2019). But “using ….” does not recognize the package name.
Can somebody help? I followed all the steps in this article. – Thank you.
I just downloaded IKVM-8.7.1-bin-net472.zip from get hub, which folder do I use?
Hi!
About 6 years ago I retired and haven’t programmed since. At that time I converted a Java Project to a dll with great success using this syntax:
ikvmc.exe -reference:E:\JavaProject\IKVM_DLLs\*.dll -out:E:\JavaProject\IKVM_DLLs\XXXXXXXXX.dll -target:library E:\JavaProject\ReferenceLibs\XXXXXXXXX.jar
I don’t understand the folder structure(unzipped IKVM-8.7.1-bin-net472.zip), which folder do I execute from?
Thank you very much.