0

I have researched this, but the results are either 10+ years old and do not explain what JNI is or if it is the only approach, or the results are for C++ or C#. So here is my question:

How do I run a Java program from a C program, using the following code as an example? What specific changes to I make to the following code to get the C program to successfully call the Java program with parameters?

In the CentOS terminal, I am able to successfully run a Java program when I type the following in the command line:

java -cp . my.package.SomeClass 1 2 3

Similarly, from the same folder in the terminal, I am able to successfully run a C program when I type the following in the command line:

./hello  

The code for hello.c is:

#include <stdio.h>

main() {
    printf("Hello World from C!\n");
}  

How do I modify the code for hello.c so that it also runs my.package.SomeClass with the parameters 1 2 3 ?

For example, how do I accomplish the following, but without throwing errors:

#include <stdio.h>

main() {
    printf("Hello World from C!\n");
    java -cp . my.package.SomeClass 1 2 3  //What is the right syntax here?
}  

Edit

I am most interested in answers that show how to call a method, such as SomeClass.someMethod(1,2,3).

halfer
  • 19,824
  • 17
  • 99
  • 186
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • Rolled back as you changed the question completely, rendering the existing answers incomplete (including the accepted one). – too honest for this site Aug 21 '15 at 01:54
  • The edit is answered by the answers below which state the library required and point to further research. Keeping the edit will help people like me who are forced to write brief C code and need a quick synopsis. – CodeMed Aug 21 '15 at 03:03
  • Quite arrogant your C-bashing. You should first think if you might offend people who actually prefer C for their projects. Extending a question is against the policies on SO. – too honest for this site Aug 21 '15 at 11:55
  • I am not bashing C. But everyone occasionally needs to edit a short snippet in a language they almost never use. – CodeMed Aug 21 '15 at 20:44
  • Maybe. But in such a case she could use a more neutral wording. – too honest for this site Aug 22 '15 at 11:10
  • @Olaf Please state the wording that you find offensive along with your suggestions for replacing it. – CodeMed Aug 22 '15 at 16:05
  • 1
    @Olaf This does not in ANY way answer the other question. Please remove the vandalism duplicate mark from the other post so that someone can answer it if you are not able to. – CodeMed Aug 27 '15 at 03:45

3 Answers3

3

If all you want to do is run java -cp . my.package.SomeClass 1 2 3 from your program, you could just use system which runs an external command:

system("java -cp . my.package.SomeClass 1 2 3");

This is the simplest solution, but not very flexible - you can't interact with the Java program apart from giving it arguments, for example.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • This is great, but for the sake of others in the future, can you simply everyone's research by also showing how to run a method of the java class with parameters, as shown in the edit I just added t my OP? – CodeMed Aug 21 '15 at 01:04
  • 1
    @CodeMed You can't do that directly without JNI. JNI is an interface that lets you access a JVM from C. – user253751 Aug 21 '15 at 01:11
  • I am marking yours as the answer because it is the shortest code. One final request, can you show how 1, 2, and 3 would look like as variables instead of constants. Variables might be more useful to others who read this in the future also. Thank you. – CodeMed Aug 21 '15 at 01:22
  • @CodeMed: If you cannot even use `snprintf` or similar, perhaps it is better not to use C? – too honest for this site Aug 21 '15 at 01:56
  • @Olaf I am forced to use C for a couple linux configs. I am just setting it up so java can do the heavy lifting. – CodeMed Aug 21 '15 at 03:05
2

You do need to use JNI (as far as I know). I use the documentation here all the time:

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/

It's a bit terse but it's workable. It handles C and C++. I don't know C#, so I cannot comment on that (although if forced to guess I would say no to C#).

Running your java program from C will require you to start a JVM programmatically using JNI functions, then find your class, then find your method, then call it. There are examples here to get you started:

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html#wp9502

If you need more help, you might need to find someone who's done it before to walk you through the steps.

Brick
  • 3,998
  • 8
  • 27
  • 47
2

I believe you are looking for the exec family of functions. Something like,

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
        const char *file = "java";
        const char *arg = "-cp . my.package.SomeClass 1 2 3";
        printf("Hello World from C!\n");
        execlp(file, arg, (char *) NULL);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • This is great, but for the sake of others in the future, can you simply everyone's research by also showing how to run a method of the java class with parameters, as shown in the edit I just added t my OP? – CodeMed Aug 21 '15 at 01:04
  • 3
    @CodeMed That is a very different question. The Java VM is generally written in C++, so (afaik) you'll need to start by writing a lot of shim code AND [JNI](http://stackoverflow.com/questions/15560572/using-jni-to-execute-a-java-jar-from-a-c-program-using-g-or-eclipse). Alternatively, you could use a RPC mechanism like SOAP, ReST, CORBA, Thrift or Sockets. – Elliott Frisch Aug 21 '15 at 01:07