代码备份,仅供参考
自述文件
# CSharpCallJava C# invoke Java via C++ as a wraper. C# invoke C++ via P/invoke. C++ starts a JVM to run the Java code. C# code should be compiled in .NET core 2.0 You should edit the Makefile to set the Path of Java SDK export LD_LIBRARY_PATH=/usr/lib/jvm/java-1.9.0-openjdk-amd64/lib/amd64/server or add it into your enviroment # Compile: javac MyTest.java make dotnet build
cscalljava.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> </Project>
init.cs
using System; using System.Runtime.InteropServices; namespace invoke { class Program { [DllImport("libc.so.6")] private static extern int getpid(); // You should edit the path of you .so lib [DllImport("./wraper.so",EntryPoint="Invokejava")] private static extern int Invokejava(string message); static void Main(string[] args) { int pid= getpid(); Console.WriteLine(pid); Console.WriteLine("Hello World!"); int status= Invokejava("Hi C# to cpp"); Console.WriteLine(status); } } }
Makefile
wraper.so:wraper.cpp g++ -shared -o wraper.so -L/usr/lib/jvm/java-1.9.0-openjdk-amd64/lib/amd64/server -I/usr/lib/jvm/java-9-openjdk-amd64/include -I/usr/lib/jvm/java-9-openjdk-amd64/include/linux/ ./wraper.cpp -fPIC -ljvm export LD_LIBRARY_PATH=/usr/lib/jvm/java-1.9.0-openjdk-amd64/lib/amd64/server
MyTest.java
public class MyTest { private static int magic_counter=777; public static void sayHi() { // <=== We will call this System.out.println("Hello, World from java"); System.out.println(magic_counter); } public static int Square(int n){ return n*n; } }
wrapper.cpp
#include <jni.h> #include <iostream> #include <cstdio> using namespace std; extern "C"{ int Invokejava(const char* message) { JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine) JNIEnv *env; // Pointer to native interface //================== prepare loading of Java VM ============================ JavaVMInitArgs vm_args; // Initialization arguments JavaVMOption* options = new JavaVMOption[1]; // JVM invocation options options[0].optionString = "-Djava.class.path=."; // where to find java .class vm_args.version = JNI_VERSION_1_6; // minimum Java version vm_args.nOptions = 1; // number of options vm_args.options = options; vm_args.ignoreUnrecognized = false; // invalid options make the JVM init fail //=============== load and initialize Java VM and JNI interface ============= jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); // YES !! delete options; // we then no longer need the initialisation options. if (rc != JNI_OK) { // TO DO: error processing... cin.get(); exit(EXIT_FAILURE); } //=============== Display JVM version ======================================= cout << "JVM load succeeded: Version "; jint ver = env->GetVersion(); cout << ((ver>>16)&0x0f) << "."<<(ver&0x0f) << endl; jclass cls2 = env->FindClass("MyTest"); if(cls2== nullptr){ cerr<<"ERROR : class not find"; } else{ printf("%s",message); cout<<"Class MyTest found"<<endl; jmethodID mid= env->GetStaticMethodID(cls2,"sayHi","()V"); if(mid==nullptr) cerr<<"ERROR : method void sayHi() not found!"<<endl; else{ env->CallStaticVoidMethod(cls2,mid); } } jmethodID mid2 = env->GetStaticMethodID(cls2,"Square","(I)I"); if(mid2==nullptr){ cerr<<"ERROR: method Square(int) not find!"<<endl; } else{ int i; cout<<"input a number"<<endl; cin>>i; cout<<"get Square return = "<< env->CallStaticIntMethod(cls2,mid2,(jint)i); cout<<endl; } // TO DO: add the code that will use JVM <============ (see next steps) jvm->DestroyJavaVM(); cin.get(); return 0; } }
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。