1

I am attempting to write a program that will read the output from a java .jar file and also give it input from time to time. Basically I am hoping to make a program that will execute certain functions when certain output is encountered.

The .jar file wasn't created by me, and I don't have the source code, so I have to use it as-is. After doing some research I decided using fork() and execl() in conjunction with pipes was the method I would need to use and I created a small program that successfully does this with a hello world program. Just as I was getting ready to modify it to run the .jar program I realized that the .jar isn't executable on its own, I have to execute it through java and so now I'm unable to figure out how to make execl() work with the .jar.

I have tried to figure out how to make execl() run java and use an argument that specifies the jar file to execute like so:

execl("java","java","jar myprog.jar",NULL);

I don't know if this method will work, though. If it will I don't know what path to use. I have seen some people recommend using JNI for similar purposes, but none of them seemed to quite fit with what I was doing and after researching it a bit I'm not totally sure that would be the best way to go. popen() seems like another possibility, but I have yet to find anything that explains how to use it well.

Any advice would be greatly appreciated.

Morvader
  • 2,317
  • 3
  • 31
  • 44
Alex
  • 671
  • 3
  • 11
  • 25

3 Answers3

1

You would start it with:

java -jar myprog.jar

Since that's the command line, you would need ...

execl("java", "java", "-jar", "myprog.jar", NULL);

(Provided java is in your path. If it's not you'll need the full path to java.)

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • I know that is how I need to do it in the console, but how would I do that with execl()? – Alex Mar 11 '11 at 07:24
  • Okay, I see now. My main problem is that I didn't realize that "-jar myprog.jar" should be split into two seperate arguments. That makes sense, though. I tried it this way and it didn't work, though. The suggestion to use execlp instead of execl did work, though. Thanks a lot for your help. – Alex Mar 11 '11 at 07:51
1

If you want to call various Java functions from within a C++ environment, you will need to use JNI. Create a Java virtual machine inside your C++ program and use it to load the jar. You can then call functions that jar exposes.

Here is a decent summary. http://www.codeproject.com/KB/cpp/CJniJava.aspx

Null Set
  • 5,374
  • 24
  • 37
  • I don't know that I want to call any functions from the jar. I just want to capture the output and give it input. I am hoping there is an easier way to do that than to use JNI, but I will be looking into it as a possibility. – Alex Mar 11 '11 at 07:35
1

For finding the java executable: if you want to find it in PATH you can use execlp instead of execl. Otherwise you can make it configurable (configuration file or env. variable). For the jar file you can look for it in the current directory or again a configurable location.

E.g., for java in PATH and myprog.jar in the current working directory:

ret = execlp("java", "java", "-jar", "myprog.jar", (char *)0);

pktoss
  • 46
  • 1
  • I just tried to do this and it didn't work the way you have put it here. It is probably because it doesn't know where to find java. I was hoping that since I can just put java in the terminal from anywhere this might work just using "java", but maybe I just need to figure out the proper path to use. – Alex Mar 11 '11 at 07:42
  • Actually, I looked over my code again and I had typed it in wrong. The way you have suggested doing it here seems to have worked perfectly. – Alex Mar 11 '11 at 07:50