代码备份,仅供参考
自述文件
# 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; } }
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2019-12-29 pip设置安装源
2019-12-29 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系