Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Java functions from C++?

Tags:

java

c++

How can I call Java functions from a C++ application?

I know about calling them from CMD (or similar techniques), but I would rather not use them.

like image 999
user63898 Avatar asked May 04 '09 10:05

user63898


People also ask

Can I call Java from C++? Can I call C++ from Java?

The Java Native Interface (JNI) is a standard to integrate in a portable way C++ and Java code. It works in both directions: you can call a C++ library from Java or you can call Java components from C++. In this tutorial, I'll explain the second approach.

How do you call a function in Java program?

To call a method in Java, write the method's name followed by two parentheses () and a semicolon; The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. You have called me!

Can we call Java method from SQL query?

To invoke a Java static method with a SQL CALL, you must publish it with a call specification. A call specification defines for SQL which arguments the method takes and the SQL types it returns. In SQL*Plus, connect to the database and define a top-level call specification for Hello.


2 Answers

As an example, check Creating a JVM from C. It shows a sample procedure to create a JVM and invoke a method. If the JVM already exists; e.g. your C program is invoked by the Java program (callback situation), you can cache the JNIEnv* pointer.

As an advice, be careful caching pointers to the JVM from C/C++, there are some semantics involved as to what you can cache and it could be invoked later on. For that, as Brian Agnew pointed out, check the JNI reference.

like image 76
Daniel H. Avatar answered Sep 22 '22 04:09

Daniel H.


Check out the JNI Invocation interface. This will allow you to embed a JVM within your C (or C++) application.

Note that various easier mechanisms exist to facilitate calling C/C++ from Java (e.g. JNA). It may be worth considering inverting your problem such that you can call from Java (I understand this may well not be possible for your particular application, however)

like image 41
Brian Agnew Avatar answered Sep 21 '22 04:09

Brian Agnew