Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking C++ code from Java (GCJ)

I'm trying to invoke C++ from Java using GCJ using the CNI, so far I'm able to invoke Java code from C++. How can I invoke C++ from Java using the CNI?

like image 790
Joe D Avatar asked Jun 08 '10 12:06

Joe D


People also ask

Does GCC support Java?

Within GCC, it comprises the Java programming language front-end (gcc/java), a runtime library (libjava) and other helper libraries (boehm-gc, libffi and zlib). Most of the runtime library implementation is shared with the GNU Classpath project.

Can you compile Java to machine code?

Rather than make you go write machine code, we use a program called a compiler. A compiler translated code written in one language to that written in another language. Thus, a compiler converts Java programs to machine code, which can then be run on the computer.

Does MinGW support Java?

Support for Windows using MinGW is also available. It can compile Java source code to either Java bytecode (class files) or native machine code. It can also compile Java bytecode to native machine code.

Can GNU compile Java?

The GNU Compiler for the JavaTM Programming Language. GCJ is a portable, optimizing, ahead-of-time compiler for the Java Programming Language. It can compile Java source code to Java bytecode (class files) or directly to native machine code, and Java bytecode to native machine code.


1 Answers

I'll extend somewhat on pcent's answer to read the GCJ/CNI Docs.

I believe that the key is to understand the whole "CNI C++ class" concept.

  • The creation of a CNI C++ class is explained in that page.
  • You can call Java code from a CNI C++ class (provided you have generated header files) - that's what you already do (calling Java from C++).
  • The key point is that CNI C++ methods can be invoked from Java (because these classes have a set of restrictions upon them to make them Java-compatible). And because the CNI class is only a (particular kind of) C++ class, you can also link it to other libraries just as you would link any C++ class.

Most of the documentation describes how the Java conventions translate in a CNI C++ class.

like image 129
Jean Hominal Avatar answered Sep 23 '22 11:09

Jean Hominal