java 如何从另一个java程序调用一个java程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12744253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:02:09  来源:igfitidea点击:

How to call a java program from another java program?

java

提问by user1722639

Possible Duplicate:
how to compile & run java program in another java program?

可能的重复:
如何在另一个 java 程序中编译和运行 java 程序?

eg if i have A.java and B.java then i want to compile and run B.java using A.java.

例如,如果我有 A.java 和 B.java,那么我想使用 A.java 编译和运行 B.java。

回答by AlexR

First, compile your code. I do not think you reallywant to compile class B from class A as you have written. This almost does not make any sense.

首先,编译你的代码。我不认为你真的想像你写的那样从 A 类编译 B 类。这几乎没有任何意义。

Now, since both are java classes you can just call methods of one class from another directly. If however your reallymean that 2 classes are independent programs, so that each one has its own main method you can run one application from another using either Runtime.getRuntime().exec(...)or using ProcessBuilder.

现在,由于两者都是 java 类,您可以直接从另一个类调用一个类的方法。但是,如果您真的意味着 2 个类是独立的程序,因此每个类都有自己的主要方法,您可以Runtime.getRuntime().exec(...)使用ProcessBuilder.

Please pay attention on words reallyI wrote. I am pretty sure you do not want to call one java program from another. Most chances are that you want to call methods of one class from another, so do this.

请字讲究我写的。我很确定您不想从另一个调用一个 Java 程序。大多数情况下,您想从另一个类调用一个类的方法,所以这样做。

回答by Dhirendra Kumar Singh

@AlexR: IMO this is a valid scenario. Let's assume you want to upload a code from some where and then execute it, and validate the output.

@AlexR:IMO 这是一个有效的场景。假设您想从某个位置上传代码,然后执行它,并验证输出。

Try using the below mentioned code:

尝试使用下面提到的代码:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    public class A {

      public static void main(String[] args) {
        try {
            Process processCompile = Runtime.getRuntime().exec("javac B.java");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Process processRun = null;
        try {
            processRun = Runtime.getRuntime().exec("java B");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            printLines(" stdout:", processRun.getInputStream());
            printLines(" stderr:", processRun.getErrorStream());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


      }

      private static void printLines(String name, InputStream ins) throws Exception {
          String line = null;
          BufferedReader in = new BufferedReader(new InputStreamReader(ins));
          while ((line = in.readLine()) != null) {
              System.out.println(name + " " + line);
          }
        }
    }