0

In an application I am writing, I am launching another application (a runnable JAR) using Runtime.exec(...). Everything launches successfully in Windows, but Linux (specifically certain installations of CentOS - works in Ubuntu) has been giving me some problems. For some reason, the only way the secondary application will successfully launch is if I execute the first through a terminal. All behavior works as expected. However, if I launch the first application by double-clicking its icon (without a terminal open), the button to launch the second application seems to do nothing. I get no exceptions or error output - just a flash of my progress bar saying that it is launching, and then nothing. I can confirm through jconsole that the second application's process is never launched.

I have seen the commonly linked article on the pitfalls of the exec method ( http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html ), but have not been able to solve this problem with anything I have found there. I am in fact reading the output and error streams of the second process, as I see all output when it successfully runs (after launching the first application through a terminal command). Not knowing a lot about deeper workings of Linux, I think this sounds like it may be a permissions issue with the output stream or something, but I am not sure.

In case it helps to diagnose the problem, I am using the command: rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."}); Since this works (depending on how the application is launched), I'm not too concerned that anything is wrong with this piece of code...

Anyone have any suggestions? Thanks in advance!

EDIT: The solution was to fix the directory to the JAR I was attempting to run. When launched via the GUI, user.dir was pointing to the parent directory of the folder containing my application. Since I'm using Eclipse RCP, my solution was to use String currDirPath = Platform.getInstallLocation().getURL().toString(); instead. Thanks for the help everyone!

2 Answers2

2

Since you're just using the jar file name - myjarfile.jar - and not the full path to it, depending on the current working directory, the jar may or may not be found. Try changing your exec command to use the full path to the jar instead. You can debug this by using rt.exec() to write the output of 'pwd' to a text file.

Rajesh J Advani
  • 5,585
  • 2
  • 23
  • 35
  • Thanks for the input. I'll debug this to make sure, but I don't think this is my problem. In posting my code sample, I left out that I use 'String jarLoc = System.getProperty("user.dir") + "/myfolder/myjarfile.jar";' to build the absolute path to the jar, based on the working directory. – FlammableChimp Jul 25 '12 at 12:55
  • The current working directory ("user.dir") is probably not what you think it is when clicking an icon to start the program. – Jonathan Callen Jul 25 '12 at 13:12
  • Rajesh and Jonathan - Very helpful! I realized that `user.dir` was pointing to the parent directory of the containing folder, rather than the folder itself, when being run via the GUI. Fixing this solved my problem. – FlammableChimp Jul 25 '12 at 14:47
0

instead of

 rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."});

use

rt.exec(new String[]{"\bin\bash", "-c", "/***path to java***/java -jar myjarfile.jar myArg1 myArg2 ..."});