1

I need to execute a jar file from inside of some C++ code. The jar file reads in some data from a text file, and then writes out some information into a different text file.

I thought

(void)system("filename.jar");

would do it, but it doesn't seem to.

4
  • 1
    Google 'start java program', take the 2nd hit. Commented Mar 11, 2011 at 7:11
  • 1
    @Hans Passant: Is that before or after this question shows up in Google? Commented Mar 11, 2011 at 8:39
  • @HansPassant In the future, would you kindly link to the page you refernced? Google search results are not the same for every user, and they tend to change over time. Commented Jan 7, 2013 at 17:19
  • If an answer can be obtained with a simple google query and you cannot figure out which of the top three hits solve your problem then the odds that an SO answer would help are exceedingly small. Commented Jan 7, 2013 at 17:22

2 Answers 2

3

If you want to simply call the jar, you should invoke java with proper command line java -jar "filename.jar"

A better way, if you need real integration between c++ and java, is to use the java native interface (jni), but for that you must be able to modify the sources in the jar file.

Sign up to request clarification or add additional context in comments.

1 Comment

Be aware that JNI works in two different directions - you can call into native code from Java (which of course requires the Java code to make those native calls, so if it doesn't already do that then it would need to be modified). Or you can create and manage a JVM from native code, I believe that also falls under the banner of "JNI". That doesn't require anything special from the Java code, and is probably all that would be used here. Arguably the java executable itself is a handy command-line version of this.
2

You need to call the java vm/interperter

system("java filename.jar");

1 Comment

And you also have to use the -jar option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.