├── cscalljava.csproj
├── Makefile
├── MyTest.java
├── README.md
├── init.cs
└── wraper.cpp
/cscalljava.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | wraper.so:wraper.cpp
2 | 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
3 | export LD_LIBRARY_PATH=/usr/lib/jvm/java-1.9.0-openjdk-amd64/lib/amd64/server
4 |
--------------------------------------------------------------------------------
/MyTest.java:
--------------------------------------------------------------------------------
1 | public class MyTest {
2 | private static int magic_counter=777;
3 |
4 | public static void sayHi() { // <=== We will call this
5 | System.out.println("Hello, World from java");
6 | System.out.println(magic_counter);
7 | }
8 | public static int Square(int n){
9 | return n*n;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CSharpCallJava
2 | C# invoke Java via C++ as a wraper.
3 |
4 | C# invoke C++ via P/invoke. C++ starts a JVM to run the Java code.
5 |
6 | C# code should be compiled in .NET core 2.0
7 |
8 | You should edit the Makefile to set the Path of Java SDK
9 | export LD_LIBRARY_PATH=/usr/lib/jvm/java-1.9.0-openjdk-amd64/lib/amd64/server
10 | or add it into your enviroment
11 | # Compile:
12 | javac MyTest.java
13 |
14 | make
15 |
16 | dotnet build
17 |
--------------------------------------------------------------------------------
/init.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 | namespace invoke
4 | {
5 | class Program
6 | {
7 | [DllImport("libc.so.6")]
8 | private static extern int getpid();
9 | // You should edit the path of you .so lib
10 | [DllImport("./wraper.so",EntryPoint="Invokejava")]
11 | private static extern int Invokejava(string message);
12 |
13 | static void Main(string[] args)
14 | {
15 | int pid= getpid();
16 | Console.WriteLine(pid);
17 | Console.WriteLine("Hello World!");
18 | int status= Invokejava("Hi C# to cpp");
19 | Console.WriteLine(status);
20 |
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/wraper.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | using namespace std;
5 | extern "C"{
6 | int Invokejava(const char* message)
7 | {
8 | JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine)
9 | JNIEnv *env; // Pointer to native interface
10 | //================== prepare loading of Java VM ============================
11 | JavaVMInitArgs vm_args; // Initialization arguments
12 | JavaVMOption* options = new JavaVMOption[1]; // JVM invocation options
13 | options[0].optionString = "-Djava.class.path=."; // where to find java .class
14 | vm_args.version = JNI_VERSION_1_6; // minimum Java version
15 | vm_args.nOptions = 1; // number of options
16 | vm_args.options = options;
17 | vm_args.ignoreUnrecognized = false; // invalid options make the JVM init fail
18 | //=============== load and initialize Java VM and JNI interface =============
19 | jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); // YES !!
20 | delete options; // we then no longer need the initialisation options.
21 | if (rc != JNI_OK) {
22 | // TO DO: error processing...
23 | cin.get();
24 | exit(EXIT_FAILURE);
25 | }
26 | //=============== Display JVM version =======================================
27 | cout << "JVM load succeeded: Version ";
28 | jint ver = env->GetVersion();
29 | cout << ((ver>>16)&0x0f) << "."<<(ver&0x0f) << endl;
30 | jclass cls2 = env->FindClass("MyTest");
31 | if(cls2== nullptr){
32 | cerr<<"ERROR : class not find";
33 | }
34 | else{
35 | printf("%s",message);
36 | cout<<"Class MyTest found"<GetStaticMethodID(cls2,"sayHi","()V");
38 | if(mid==nullptr)
39 | cerr<<"ERROR : method void sayHi() not found!"<CallStaticVoidMethod(cls2,mid);
42 | }
43 | }
44 | jmethodID mid2 = env->GetStaticMethodID(cls2,"Square","(I)I");
45 | if(mid2==nullptr){
46 | cerr<<"ERROR: method Square(int) not find!"<>i;
52 | cout<<"get Square return = "<< env->CallStaticIntMethod(cls2,mid2,(jint)i);
53 | cout<DestroyJavaVM();
58 | cin.get();
59 | return 0;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------