Computing.Net > Forums > Programming > java system call to C++ program

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

java system call to C++ program

Reply to Message Icon

Name: Infinite Recursion
Date: April 7, 2004 at 05:48:01 Pacific
OS: Solaris
CPU/Ram: na
Comment:

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());
}

}



Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: April 7, 2004 at 05:58:47 Pacific
Reply:

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);
}
}



0
Reply to Message Icon

Related Posts

See More


Macromedia Flash MX read with non_block in Wi...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: java system call to C++ program

Display a system call in C++ www.computing.net/answers/programming/display-a-system-call-in-c/10868.html

C Program System() www.computing.net/answers/programming/c-program-system/15348.html

System call in C www.computing.net/answers/programming/system-call-in-c/661.html