jen2483
asked on
How to run external program in java?
I try to run sqlldrin my java code. I wrote a small to test if exec is working. The code is as following:
try {
System.out.println("Starti ng");
Process p = Runtime.getRuntime().exec( "ping 127.0.0.1");
p.waitFor();
if ( p.exitValue() ==0)
{
System.out.println("done") ;
}
}catch (IOException ioe){
System.out.println(ioe);
}catch (InterruptedException ie){
System.out.println(ie);
}
It doesn't work. It seems it will stop there forever.I run it on Windows 2000. Please help me figure out why?
Thanks.
try {
System.out.println("Starti
Process p = Runtime.getRuntime().exec(
p.waitFor();
if ( p.exitValue() ==0)
{
System.out.println("done")
}
}catch (IOException ioe){
System.out.println(ioe);
}catch (InterruptedException ie){
System.out.println(ie);
}
It doesn't work. It seems it will stop there forever.I run it on Windows 2000. Please help me figure out why?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it works fine on my XP machine
this is a version i found in my archives that echoes the output of the ping:
import java.io.*;
class ProcessReadOut {
public static void main(String[] args) {
try {
String command = "ping localhost";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(command);
InputStream in = p.getInputStream();
InputStreamReader inReader = new InputStreamReader(in);
BufferedReader bInReader = new BufferedReader(inReader);
String currRead;
while ((currRead = bInReader.readLine()) != null) {
System.out.println("from java -> " + currRead);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.*;
class ProcessReadOut {
public static void main(String[] args) {
try {
String command = "ping localhost";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(command);
InputStream in = p.getInputStream();
InputStreamReader inReader = new InputStreamReader(in);
BufferedReader bInReader = new BufferedReader(inReader);
String currRead;
while ((currRead = bInReader.readLine()) != null) {
System.out.println("from java -> " + currRead);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
there's also an example of reading exec() output at:
http://www.objects.com.au/java/examples/util/ConsoleExec.do
http://www.objects.com.au/java/examples/util/ConsoleExec.do
http://www.idevelopment.info/data/Programming/java/PROGRAMMING_Java_Programming.shtml
in that check out Running External OS Commands
the following is the program from the site which u can use
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* Created on Oct 11, 2004
*
*/
public class TestCode {
public static void main(String[] args) {
String s = null;
try {
// run the Unix "ps -ef" command
Process p = Runtime.getRuntime().exec(
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInp
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErr
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
} catch (IOException e) {
System.out.println("[IOExc
e.printStackTrace();
System.exit(-1);
}
}
}