-1

Possible Duplicate:
Run Java program into another Program

i try to run jar file from java application which was created in eclipse. when i run jar file using below source code then fire Unable to access jarfile error

Process process = run.exec("java -jar  TestJava.jar");
InputStream inError = process.getErrorStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inError));
System.out.println("Error=" + bufferedReader.readLine());
Community
  • 1
  • 1
user1508234
  • 7
  • 1
  • 5

4 Answers4

1

Sounds like TestJava.jar is in a different directory. If you're running this code within Eclipse, then the present working directory is going to be the same as your Eclipse project's folder (unless you've configured it differently, this is Eclipse's default run location). Either specify a path to TestJava.jar via an absolute path or a path relative to the present working directory.

One other thing you'll need to be mindful of - you need to consume both the error stream and the output stream of the Process you're creating. The default output/error stream buffer sizes of Process instances are not very big and, if full, will cause that Process to block indefinitely for more buffer space. I recommend consuming each stream in a separate Thread.

CodeBlind
  • 4,519
  • 1
  • 24
  • 36
0

Set the working directory where this process should run. Set the working directory using the below statement.

Runtime.getRuntime().exec(command, null, new File("path_to_directory"));
pratikabu
  • 1,672
  • 14
  • 17
0

First try the java -jar command on cmd window and see if you can run TestJava.jar. Then try running the same command from your code.

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("java -jar TestJava.jar");
System.exit(0);
Manisha Mahawar
  • 627
  • 6
  • 9
0

You need to supply the full path to the Java command.

For example, under windows you'd need something like

C:/Program Files/java/jre/bin/java.exe

ProcessBuilder & Runtime.exec don't take the system path into account when trying to execute your command

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366