跨语言调用

Java 跨语言调用

JNA provides Java programs easy access to native shared libraries without writing anything but Java code - no JNI or native code is required. This functionality is comparable to Windows’ Platform/Invoke and Python’s ctypes.

C++作为中转

Java 调用 C# DLL

jni4net:bridge between Java and .NET (intraprocess, fast, object oriented, open-source)

Quick Start

Installation

Microsoft Windows (XP / 7 / 8 / 8.1) x86 or x64 This guide is currently using Microsoft Windows 7 Professional x64 and is confirmed working. You may try other versions of Windows. Jni4Net binaries and dependencies do not support Mac/Linux.

Microsoft Visual Studio Microsoft Visual Studio 2010 Ultimate will work.

Microsoft .NET Framework 4 Version 4.0.30319 will work. Minimum version is 2.0.50727. Can try later versions.

Java SE Development Kit You may follow the download links and install the 32-bit version, and the rest of this process should go smoothly.

Set up your JAVA_HOME environment variable. Simply follow the instructions found on the net.

Add JAVA_HOME to your system Path by creating a new entry for JAVA_HOME, ;%JAVA_HOME%\bin;.

Java commands can now be run from Windows Command Prompt. Remember to separate entries with semicolons (;) in between, then restart your computer.

Download Jni4Net Currently this guide uses jni4net-0.8.8.0-bin.zip.

ProxyGen

Open and extract these files to your favorite location, (the Desktop for example): bin/proxygen.exe bin/proxygen.exe.config lib/jni4net.j-0.8.8.0.jar lib/jni4net.n.w32.v40-0.8.8.0.dll lib/jni4net.n-0.8.8.0.dll Because proxygen.exe is causing confusion between 32-bit and 64-bit versions of Java and CLR virtual machines, the following steps are required to modify the file for easier use. Open the Visual Studio Command Prompt. You can find it in the Start Menu via All Programs > Microsoft Visual Studio (*) > Visual Studio Tools or simply type Visual Studio Command Prompt. Once opened, enter: corflags c:\users\you\desktop\proxygen.exe /32BIT+ /Force, (command and arguments are case-sensitive). If the Command Prompt gives warning CF011, you may ignore it. The tool is ready for use in development.

Check the ProxyGen Setup guide if you haven’t.

We will also refer to Jni4Net version 0.8.8.0 for the time being.

[The assembly of your choice]. For this guide, we will refer to a sample .NET-assembled library called testlib.dll. It looks like this:

        // Simple method that just prints a hard-coded string.
        public void Hello()
        {
            Console.WriteLine("Hello Java, from .NET!");
        }

        // Parameterized method that accepts a string
        public void Repeat(string line)
        {
            Console.WriteLine(line);
        }

Open the Windows Command Prompt.

Enter a command to proxygen.exe as follows:

Enter a command to proxygen.exe as follows: c:\users\you\desktop\proxygen.exe c:\users\you\desktop\testlib.dll -wd c:\users\you\desktop

Four files and folders in total will be created:

clr\

clr
jvm\

clr
jvm
testlib.proxygen.xml

clr
jvm
testlib.proxygen.xml build.cmd

Click and run the newly generated build.cmd file. Your .NET and Java proxies are created:

testlib.j4n.dll

testlib.j4n.dll testlib.j4n.jar

testlib.j4n.dll testlib.j4n.jar target\ included

Ready the following resources to be imported into your project:

jni4net.j-0.8.8.0.jar

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testlib.dll

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testlib.dll testlib.j4n.jar

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testlib.dll testlib.j4n.jar testlib.j4n.dll

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testlib.dll testlib.j4n.jar testlib.j4n.dll jni4net.n.w32.v40-0.8.8.0.dll

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testlib.dll testlib.j4n.jar testlib.j4n.dll jni4net.n.w32.v40-0.8.8.0.dll target\ (optional)

*Try not to include the rest of the generated files and folders as that would just increase the size of your project explorer/pipeline.


JARs - Quick Example with testjar.jar

Running proxygen.exe with the command,

Running proxygen.exe with the command, c:\users\you\desktop\proxygen.exe c:\users\you\desktop\testjar.jar -wd c:\users\you\desktop

generates these to your desktop:

clr\

clr
jvm\

clr
jvm
testjar.proxygen.xml

clr
jvm
testjar.proxygen.xml build.cmd

Run build.cmd and receive,

testjar.j4n.dll

testjar.j4n.dll testjar.j4n.jar

testjar.j4n.dll testjar.j4n.jar target\

then import these into your project:

jni4net.j-0.8.8.0.jar

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testjar.jar

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testjar.jar testjar.j4n.jar

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testjar.jar testjar.j4n.jar testjar.j4n.dll

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testjar.jar testjar.j4n.jar testjar.j4n.dll jni4net.n.w32.v40-0.8.8.0.dll

jni4net.j-0.8.8.0.jar jni4net.n-0.8.8.0.dll testjar.jar testjar.j4n.jar testjar.j4n.dll jni4net.n.w32.v40-0.8.8.0.dll target\

上一页
下一页