One thing that is often discussed in various groups is how .NET calls the implementation of Java. One of the most common scenarios is the key provided by Java in terms of encryption and decryption. C# cannot decrypt it. The byte range in C# is [0,255], and The byte range in Java is [-128,127]. Since the password generator is unique to Java, other languages (IOS, ANDROID, C#, .NET, etc.) are not supported. Since Java is so unique, we use the The solution is then Java-style.
Java and .NET are the two main technologies for software development today, and Java 8, albeit an older technology, has done a lot of work. So using libraries created in Java in .NET is a very common practice when it comes to reusability. Assuming you have developed a library in Java and want to use it in a .NET project, then IKVM.NET helps to use the Java library in .NET.
This is the protagonist IKVM.NET that we are going to introduce today. The IKVM.NET project has a very long history. As early as 2006, I wrote an article on my blog " Running Java Programs on the .NET Platform - Getting Started with IKVM.NET "[ 1], IKVM.NET and Mono are both .NET open source prehistoric community projects, IKVM is a Java to .NET compiler, and Mono is an open source, cross-platform version of .NET Framework. In 2017, the author of IKVM.NET decided to give up the development of IKVM.NET. Since then, some people in the community forked IKVM.NET and continued to work. Although the development work is not very active, the development has been very active since March 2022 ( https://github.com/ikvm-revived/ikvm)[2 ], .NET 6 is now fully supported.
IKVM.NET consists of the following three main components:
- Java virtual machine implemented in .NET: It has a JVM developed using C#.NET, which provides bytecode conversion and verification, class loading and other functions, and currently supports Java 8, which is also the most used version of Java.
- .NET Implementation of Java Class Library: It basically uses the OpenJDK [3] project to implement the JDK library.
- Tools to support Java and .NET interoperability: IKVM.NET includes the following tools:
a) ikvm: Java Virtual Machine: We can compare this to java.exe ("dynamic mode"). It loads a class file and executes its main method, if we pass the class file name as a parameter then it will execute the Java code in the executable jar file. If we pass a jar file as parameter then it will execute it.
b) ikvmc: Compiles Java bytecode to CIL, used to compile Java classes and jars into .NET assemblies ("static mode"). This tool converts Java bytecode to .NET DLLs and exes. It converts the Java bytecode in the input file to a .NET DLL. So when we pass multiple jar files and class files, it will combine them together and generate a single exe or DLL file. Whether it's an exe or a DLL depends on whether the passed class and jar files have a Main method. If they have a Main method then it will generate an exe, otherwise DLL.
c) ikvmstub: Generate Java stub classes from .NET assemblies: It generates stub class files from .NET assemblies so that Java code can be compiled against .NET code. The ikvmstub tool generates Java stubs from .NET assemblies. ikvmstub reads the specified assembly and generates a Java jar file containing Java interfaces and stub classes.
IKVM.NET is useful for various software development scenarios. Below is a sample of some possibilities.
- The IKVM application included in the plug-in JVM distribution is a .NET implementation of the Java Virtual Machine. In many cases, you can use it as a drop-in replacement for Java. For example, instead of typing "java -jar myapp.jar" to run the application, you can type: ikvm -jar myapp.jar
- To use Java libraries in .NET applications, IKVM.NET includes ikvmc, a Java bytecode to .NET IL converter. If you have a Java library that you want to use in a .NET application, then run "ikvmc -target:library mylib.jar" to create "mylib.dll". For example, the Apache FOP project is an open-source XSL-FO processor written in Java that is widely used to generate PDF documents from XML sources. With IKVM.NET technology, Apache FOP can be used by any .NET application.
- Developing .NET applications in Java IKVM provides you with a way to develop .NET applications in Java. Although IKVM.NET does not include a Java compiler for .NET, you can use any Java compiler to compile Java source code to JVM bytecode and then use "ikvmc -target:exe myapp.jar" to generate .NET executable file. You can even use the .NET API in Java code using the included ikvmstub application.
▌Support Platform
- NET Framework 4.6.1 and later
- NET Core 3.1 and later
- NET 5 and above
- Java SE 8
▌IkvmReference
IKVM includes build-time support for converting Java libraries to .NET assemblies, a recently added feature, see IkvmReference Design [4]. Install the package in the project where you want to reference the Java library. Use IkvmReference to indicate which Java libraries your project needs.
example:
<ItemGroup>
<IkvmReference Include="..\..\ext\helloworld-2.0.jar" />
</ItemGroup>
The output assembly will be generated as part of the project build process. Additional metadata can be added to the IkvmReference to customize the generated assembly.
- Identity: The identity of the project can be a) the path to the JAR file b) the path to the directory or c) other trivial names. IkvmReference
- AssemblyName: By default, generated using the rules defined by the specification. To override this, do so here. AssemblyNameAutomatic-Module-Name
- AssemblyVersion: By default, it is generated using the rules defined by the specification. To override this, do so here. AssemblyVersionAutomatic-Module-Name
- DisableAutoAssemblyName: If detection is disabled. trueAssemblyName
- DisableAutoAssemblyVersion: If detection is disabled. trueAssemblyVersion
- FallbackAssemblyName: Use this value if not provided or cannot be calculated. AssemblyName
- FallbackAssemblyVersion: Use this value if not provided or cannot be calculated. AssemblyVersion
- Compile: An optional semicolon-separated list of Java classpath entries to compile into an assembly. By default, this value is the project's value if the project's identity is an existing JAR file or directory (not yet supported). Support for MSBuild clients to reference multiple JAR or .class files. Identity
- Sources: An optional semicolon-separated list of Java source files to use during documentation generation. (not yet supported)
- References: An optional semicolon-separated list of additional identity values to specify as references to the current identity value. For example, include both as items if it depends on the id specified on the metadata. IkvmReferencefoo.jarbar.jarIkvmReferencebar.jarReferencesfoo.jar
- Debug: An optional boolean indicating whether to generate debug symbols (non-portable). By default, this is determined based on the overall settings of the project.
- MSBuild item groups define all other supported metadata. Reference
IkvmReference is not transitive. Including it in one project and adding a dependency to that project from the second project does not result in the same reference being served on the second project. Instead, add a reference to each item.
Make sure it's set the same for each project you want to resolve to the same generated assembly.
<ItemGroup>
<IkvmReference Include="helloworld.jar">
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</IkvmReference>
<IkvmReference Include="helloworld-2.jar">
<AssemblyName>helloworld-2</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<References>helloworld.jar</References>
<Aliases>helloworld2</Aliases>
</IkvmReference>
</ItemGroup>
Related Links:
[1] Running Java programs on the .NET platform - Getting started with IKVM.NET:
https://www.cnblogs.com/shanyou/articles/343118.html
[2] ikvm Github resurrection repository:
https://github.com/ikvm-revived/ikvm
[3] OpenJDK:
http://openjdk.java.net/
[4] IkvmReference design scheme:
https://github.com/ikvm-revived/ikvm/issues/54
Long press to identify the QR code and follow Microsoft China MSDN
Click to go to .NET Chinese official website ~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。