Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java calling C calling Java

I want to call a C funcion from Java using JNI. And in the C function, I want to create a JVM and call some Java objects. When I try to create the JVM, JNI_CreateJavaVM returns -1.

So, I want to know if it is possible to do this. The C code is compiled to create a .so file (in linux), and the Java code calls the function in the .so file.

Any example doing Java->C->Java will be useful.

Thanks.

like image 733
arnott Avatar asked Mar 23 '11 20:03

arnott


People also ask

Can you call C from Java?

Java native interface (JNI) is a framework provided by java that enables java programs to call native code and vice-versa. Using JNI a java program has the capability to call the native C code.

What is the calling method in Java?

To call a method in Java, simply write the method's name followed by two parentheses () and a semicolon(;). If the method has parameters in the declaration, those parameters are passed within the parentheses () but this time without their datatypes specified.

What does C do in Java?

C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system. Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).

What is JNA vs JNI?

Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no boilerplate or generated glue code is required.


1 Answers

No, unfortunately its not possible. You can only have one JVM per process, and you're already in a JVM process.

like image 76
MarcB Avatar answered Sep 19 '22 03:09

MarcB