1

I have some Java (swing) code that makes a call to Runtime.getRuntime().exec(). The program runs perfectly well if I run it from the command line.

The problem is, when I'm not running from command line, the application isn't making the call to Runtime.getRuntime().exec(), so I can't execute the system commands that I want. When I double click the jar file, the GUI pops up, and everything works perfectly, except for the system call.

Here is a similar thread about a program outputting to console after double-clicking an executable jar, but it isn't quite what I'm looking for: Double Clicking JAR file does not open Command Prompt

Any ideas on how to get this to work? Any help is greatly appreciated!

I'm running Ubuntu on a VM, and I also eventually want to port this to Mac OS.

Community
  • 1
  • 1
Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52
  • Without console you do not see possible exceptions. Add [unhandled exception handler](http://stackoverflow.com/questions/588634/is-there-an-unhandled-exception-handler-in-java) or just a simple try-catch around `Runtime.getRuntime().exec()` and log (possible) exceptions to some file or display them in a dialog. – Oleg Estekhin May 30 '14 at 15:33
  • You can use java -jar /usr/a/jarfilename.jar to see if there is exceptions – Saurabh Ande May 30 '14 at 15:39
  • I have been catching exceptions and printing them as a dialog, so they would show up if Runtime.getRuntime().exec() threw any. It's an odd sort of problem, because it works perfectly fine from command line. It probably has to do something with how jar executables are invoked on linux systems. P.S. How did you format that code in your edit like that, @OlegEstekhin? – Matt Messersmith May 30 '14 at 15:55
  • Maybe the environment of the java program when started differs from the one when started through double-click. You could try to use a fully qualified path-name of the program to be started (to remove dependency on PATH) or you could try to start a _simpler_ program (like `/bin/echo`) – Volker Stampa May 30 '14 at 17:48
  • When I created the jar using the cfm option and supplying a manifest file, instead of the cfe option, my try/catch block output an error to the dialog. The first error I got, it complained that it couldn't find the binary file ("Cannot run program "./pipe": error=2, No such file or directory"). Note that this works perfectly if I call java -jar from the command line, just not double-click. Also, I tried just outputting a simple command "touch TEST". The program ran and didn't throw any exceptions, but it didn't create the TEST file anywhere (I checked in the jar too). – Matt Messersmith Jun 02 '14 at 15:55

1 Answers1

0

When you invoke an executable jar by double clicking on Ubuntu, the current working directory is your home directory, it is not the directory where the jar is located.

You were right @Volker Stampa, the absolute path name was necessary to call the executable I needed to call. I did try this immediately after you commented it, but apparently I had the wrong string for the path. Lesson learned, absolute paths are always best. Thanks for your help!

Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52