Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am using this function below to execute a script that I wrote in C++. However, it is not running and not providing an error message. The system executing part of the statement runs (because I could put "touch data1.dat" as the string and it works)... Why can I not run my C++ program with the syntax below?
public static final void test1( String abc )
{
try
{
System.out.println("Executing dir");
Process p = Runtime.getRuntime().exec("/mydir/myscript param1 param2 param3");System.out.println(abc);
}
catch (Throwable t)
{
System.out.println("ERROR: " + t.getMessage());
}}

In its simplest form, exec() is quite easy to use:
Process p = Runtime.getRuntime().exec("/bin/ls");
"The problem with this form is that it gets you nowhere. When Java forks a new process it redirects STDIN, STDOUT and STDERR. Therefore the results of "/bin/ls" do not go to the screen. Instead you must use the getInputStream(), getOutputStream() and getErrorStream() methods of the Process object to communicate with the program that you executed. "
Below is an example using the Unix ls(1) command.
import java.io.*;class execInput {
public static void main(String Argv[]) {
try {
String ls_str;Process ls_proc = Runtime.getRuntime().exec("/bin/ls -aFl");
// get its output (your input) stream
DataInputStream ls_in = new DataInputStream(
ls_proc.getInputStream());try {
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str);
}
} catch (IOException e) {
System.exit(0);
}
} catch (IOException e1) {
System.err.println(e1);
System.exit(1);
}System.exit(0);
}
}

![]() |
Macromedia Flash MX
|
read with non_block in Wi...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |