Runtime.exec another java program question

D

Dave

Hi,

Just playing around the Runtime.exec function.

Suppose I have a program called Worker.java, which will act as the
sub-process.
import java.io.*;

public class Worker {
public static void main(String[] args) {
try {
PrintWriter loFile = new PrintWriter( new
FileWriter("testout") );
loFile.println("test");
loFile.close();
} catch( Exception e) {
e.printStackTrace();
}

}
}

and the caller Caller.java
public class Caller {
public static void main(String[] args) {
String SC = "java Worker";
// String SC = "touch touchedout";
try {
Process t_proc = Runtime.getRuntime().exec( SC
);
} catch( Exception e) {
e.printStackTrace();
}
}
}

Then after compiled both and ran java Caller, I didn't get the output
file "test" generated by Worker. No errors, no complaints. Nothing. I
have permission on writting to the dir. However, if I use the "touch
touchedout" as the command to be executed by exec, I'll get the output
file.

Is there any limitation that I can't exec another java program?

Thanks a lot.
 
M

Matt Humphrey

Dave said:
Hi,

Just playing around the Runtime.exec function.

Suppose I have a program called Worker.java, which will act as the
sub-process.
import java.io.*;

public class Worker {
public static void main(String[] args) {
try {
PrintWriter loFile = new PrintWriter( new
FileWriter("testout") );
loFile.println("test");
loFile.close();
} catch( Exception e) {
e.printStackTrace();
}

}
}

and the caller Caller.java
public class Caller {
public static void main(String[] args) {
String SC = "java Worker";
// String SC = "touch touchedout";
try {
Process t_proc = Runtime.getRuntime().exec( SC
);
} catch( Exception e) {
e.printStackTrace();
}
}
}

Then after compiled both and ran java Caller, I didn't get the output
file "test" generated by Worker. No errors, no complaints. Nothing. I
have permission on writting to the dir. However, if I use the "touch
touchedout" as the command to be executed by exec, I'll get the output
file.

Is there any limitation that I can't exec another java program?

There will be some other problem, like the runtime interpreter not knowing
where to find "java". Try an absolute path. I'm not sure what the effect
is that you don't wait for the process to complete. After that, make sure
it can find the Worker.class.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top