TAGS :Viewed: 21 - Published at: a few seconds ago

[ How to run a jar file at a directory with a process ]

I am trying to use a Process in java to be able to run a jar file that is inside my AppData folder (I am running Windows 7). My code is as follows,

File directory = new File(System.getProperty("user.home") + "/AppData/Roaming" + "/Folder WithMyJar");
directory.mkdir();
Process p = Runtime.getRuntime().exec("java -jar " + directory.getPath() + "\\" + "myJar.jar" + "arg1 arg2");
System.exit(0);

When I use the same path inside command prompt the jar will work just fine; however, once I try the same thing inside eclipse it will not run nor will it give any error as to why it won't.

I should add that prior to putting the jar file within my AppData folder, I was able to run the same jar file on my desktop from eclipse.

So how would I go about making this run inside eclipse?

Answer 1


I found the problem.

File directory = new File(System.getProperty("user.home") + "/AppData/Roaming" + "/Folder WithMyJar");
directory.mkdir();
Process p = Runtime.getRuntime().exec("java -jar " + directory.getPath() + "\\" + "myJar.jar arg1 arg2");
InputStream inError = process.getErrorStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inError));
System.out.println("Error=" + bufferedReader.readLine());

Using the last 3 lines of code there for finding the error from it. Because the file I was opening was a completely different project the errors would not get printed out in eclipse because I was technically not running the project I was trying to open in eclipse, but rather on the system. So using the InputStream and BufferedReader to get the error from the process it was able to give me the error: "Unable to access jar file at C:\Home\AppData\Roaming\My " Looking at that you can tell that isn't the complete path. Because the path to the jar file has a space in it, on Windows, you HAVE TO surround the path in quotes.

Giving me the final code of:

String dir = '"' + directory.getPath() + "\\" + "myJar.jar" + '"';
Process p = Runtime.getRuntime().exec("java -jar " + dir);