Solved

How to run external program in java?

Posted on 2004-10-11
5
220 Views
Last Modified: 2010-03-31
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("Starting");
      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.

0
Comment
Question by:jen2483
5 Comments
 
LVL 4

Accepted Solution

by:
JK2429 earned 500 total points
ID: 12279462
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12279639
Check out this link
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("ping localhost");
           
            BufferedReader stdInput = new BufferedReader(new
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new
                 InputStreamReader(p.getErrorStream()));

            // 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("[IOException]. Printing Stack Trace");
            e.printStackTrace();
            System.exit(-1);
        }        
    }
}
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 12279730
it works fine on my XP machine
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 12279809
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();
    }
  }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12283260
there's also an example of reading exec() output at:

http://www.objects.com.au/java/examples/util/ConsoleExec.do
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 56
groupSum5 challenge 5 71
what is a "java.lang.System Property"   ? 20 57
object oriented programming comparison 5 55
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now