如何在eclipse中从另一个java程序执行java程序

时间:2016-04-08 20:32:12

标签: java eclipse javac

我试图从另一个java程序Demo.java运行一个名为Test.java的java程序。两个程序都在同一个程序包中,我做这样的事情:

try{
                System.out.println("Executing another client");
                runProcess("javac -cp gridgain-examples C:/Users/Desktop/gridgain/examples/src/main/java/apache/ignite/schemas/Test.java");
                System.out.println("******");
                runProcess("java -cp gridgain-examples C:/Users/Desktop/gridgain/examples/src/main/java/apache/ignite/schemas/Test.java");
            } catch(Exception e) {
                e.printStackTrace();
            }

runProcess和printlines方法是:

 private static void printLines(String cmd, InputStream ins) throws Exception {


   String line = null;
    BufferedReader in = new BufferedReader(
        new InputStreamReader(ins));
    while ((line = in.readLine()) != null) {
        System.out.println(cmd + " " + line);
    }
  }

private static void runProcess(String command) throws Exception {
    Process pro = Runtime.getRuntime().exec(command);
    printLines(command + " stdout:", pro.getInputStream());
    printLines(command + " stderr:", pro.getErrorStream());
    pro.waitFor();
    System.out.println(command + " exitValue() " + pro.exitValue());
  }

但它没有执行。请告诉我怎么做?

1 个答案:

答案 0 :(得分:0)

您是否尝试过" C:\ Users \ Desktop \ gridgain \ examples \ src \ main \ java \ apache \ ignite \ schemas \ Test.java"?

我对此发表评论,但没有声誉:(