2

I am doing a C++ program on my Raspberry Pi and I want to call a Java class from my C++ program.

I have my main program written in C++, which is the driver for my application, however, I am trying to interface with a device through my program and the communication API is written in Java. I don't want to rewrite the entire API, so my main objective is to call the API from my C++ main.

Does anyone know how this is possible?? I heard JNI is one way but I am uncertain how to accomplish this on RPI.

  • 2
    Take a look here http://stackoverflow.com/questions/992836/how-to-access-the-java-method-in-a-c-application – Balwinder Singh Oct 29 '15 at 22:49
  • I am not sure how to link the program to the additional dependencies in RPI this example uses Visual Studio. I am trying to compile this using command line. – Kenan Tufekci Oct 29 '15 at 22:58
  • Even I am not aware of what to do to call java from c++. But did some googling and this sort of links comes up on top. I am sure you will find lots more if you try searching for them – Balwinder Singh Oct 29 '15 at 23:00

1 Answers1

0

It can be done with JNI. As it happens I have a project on GitHub where I have been working on a C++ wrapper generator for Java.

https://github.com/wshackle/java4cpp

Even if you don't want to use it, some of the code or examples might be useful to take a look at.

To compile a JNI program on linux ( which I think should also work for Rasberry PI although I have never used a Rasberry PI) , I would use a command of the form:

export JAVA_HOME=/usr/local/jdk1.8.0_60
g++ -I ${JAVA_HOME}/include -I ${JAVA_HOME}/include/linux -L ${JAVA_HOME}/jre/lib/i386/server/  -Wl,--rpath ${JAVA_HOME}/jre/lib/i386/server/  cpplussource.cpp -ljvm -o newprogram

I am not sure which processor the Rasberry-PI uses. i386 might be replaced with something else. Your JDK might come with a client directory rather than a server directory.

There is more on the C++ compiler options at :

https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#compiling_loading_and_linking_native_methods

and more on invoking the virtual machine at:

https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.html#overview

WillShackleford
  • 6,918
  • 2
  • 17
  • 33
  • Wow this is amazing I will try the github that you have created and see if i can get this to work. Thanks again. By the way I have Raspberry Pi 2 (RPi2) Model B Quad-Core 900 MHz 1 GB RAM – Kenan Tufekci Oct 29 '15 at 23:51
  • When I run your simple example https://github.com/wshackle/java4cpp/tree/master/examples/simple I get the following errors: Can't find class Simple s.getValue() = simple0.cpp:194 Call of method getValue of Simple with jthis == NULL. -1 any reason why i am getting this result? I followed your instructions and everything was fine no errors. – Kenan Tufekci Oct 30 '15 at 01:11
  • I am trying to run a class file through your C++ wrapper but I am running into issues when I try to use external jar files. I have a .java file that uses external jars to call other classes and I must include those jars in the classpath when compiling. However when I run your : java -jar target/java4cpp-1.0-SNAPSHOT-jar-with-dependencies.jar className I get Exception in thread "main" java.lang.NoClassDefFoundError it does not find the classes in the external jars. How do I link the external jars when running your jar file at the same time? – Kenan Tufekci Nov 04 '15 at 06:41