3

How to execute a java program with the help of Runtime.getRuntime().exec(). For example we shall have the java file path as c:/java/abc.java. Please help me with the code.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Arun
  • 580
  • 6
  • 11
  • 17

6 Answers6

2

Assuming that abc.java contains a main method that you want to execute:

Runtime.getRuntime().exec("javac c:\java\abc.java -d c:\java\")
Runtime.getRuntime().exec("java c:\java\abc")
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127
  • CreateProcess: c:\j2sdk1.4.0\bin\helloworld error=2 This is the runtime exception message – Arun Feb 03 '09 at 07:04
  • That doesn't really give a whole lot to go off of. Can you edit your question to include a simple helloworld file and the whole stack trace? – Chris Bunch Feb 03 '09 at 07:15
2

Do not forget that:

  • you may need to read stdout/stderr of a java program
  • you may have to set/update environment variable and PATH before executing your java command

    CreateProcess: c:\j2sdk1.4.0\bin\helloworld error=2

means Win32's CreateProcess returns a 2 as error code when it cannot find the command you specify; more specifically, when the command does not refer to an executable file on its lookup path.

Look at this SO question for a more complete "Runtime.getRuntime().exec()" code, and also to this snippet.

This code creates a shell (as in Runtime.getRuntime().exec("cmd /K")), in which you write on sdtin whatever command you want to execute.

The interest of this approach is to reuse the shell process to benefit from a previous command: it you execute a 'cd', then execute a 'dir', the latter command would display the content of the directory referenced by the cd command.

The same would be true for PATH settings, just before using javac or java.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

You should use ProcessBuilder instead of Runtime. Basic usage is like:

Process process = new ProcessBuilder(command).start();

You will find more code under the link above. Also see this question.

Community
  • 1
  • 1
Fabian Steeg
  • 44,988
  • 7
  • 85
  • 112
1
String path1 = "f://" + File.separator+username+File.separator+progName; 
Runtime runtime = Runtime.getRuntime();
String command = "javac -classpath " + path + " " + path1;
System.out.println(command);
Process process = runtime.exec(command);
InputStream error = process.getErrorStream();
ndeverge
  • 21,378
  • 4
  • 56
  • 85
Naefy
  • 11
  • 1
1

You mean you want a Java program to run another Java program. This SO thread might be helpful, in that case.

Community
  • 1
  • 1
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
0

Please see the excellent resource which used to be called javaalmanac.

http://www.exampledepot.com/egs/java.lang/Exec.html

try {
    // Execute a command with an argument that contains a space
    String[] commands = new String[]{"grep", "hello world", "/tmp/f.txt"};
    commands = new String[]{"grep", "hello world", "c:\\Documents and Settings\\f.txt"};
    Process child = Runtime.getRuntime().exec(commands);
 } catch (IOException e) {
 }
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347