[Developer Notes] c# calls java code

 

 

1. Description of needs

  An algorithm implemented by java, I want to translate it into c#, after translating the code, I find that there is a bug, so I don't debug it. Directly package the jar into a dll for use.

2. Principle description

  The jar can be packaged into a dll by the ikvmc tool, and then the dll and the corresponding dl can be directly called after the dll and the corresponding dl are introduced into the project.

3. Process

  1. To package the jar, you can right-click the project through myeclipse and export it as jar. java project structure:

 

  2. Generate dll and download IKVMC tool: https://sourceforge.net/projects/ikvm/files/

    

  Just click the green button. After downloading, unzip the file, and the directory after unzipping is as follows:

  Add the bin directory to the system environment variable. This tool is used to convert the jar into a dll. After adding the variable, it is convenient to call anywhere.

  Switch the directory to the generated jar directory and run: ikvmc -out:id3d.dll id3d.jar (the dll name in the front and the jar name in the back), the generated results are as follows:

  

  The id3d.dll file is introduced into the c# project, and the IKVM.OpenJDK.Core.dll, IKVM.Runtime.dll, IKVM.Runtime.JNI.dll files in the bin directory just now are introduced.

  

  At this point, you can write c# code.

 

4. Write C# code

  

  As shown in the figure, the class can be called directly through the package name. .

  In addition, after the introduction of the above three ikvm dlls, you can also write java code directly in c#.

  Come and give us a try:

/// <summary>
        /// Copy the content of Dictionary in c# to HashMap in java
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public java.util.Map getMap(Dictionary<string, Object> dic) {
            java.util.Map ret = new java.util.HashMap ();
            foreach (KeyValuePair<string,Object> item in dic)
            {
                ret.put(item.Key,item.Value);
            }

            return ret;
        }

  

 

 

 

   Thanks to the seniors who are doing the basic technology, let us now have so many convenient tools available!

 

 

 

Attach the ikvm Baidu network disk link: https://pan.baidu.com/s/1OIc0h17jLNeDWDBRZjVNNQ Password: xz6o

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128348&siteId=291194637