【问题标题】:Not Able to see output via Java Runtime Process无法通过 Java 运行时进程查看输出
【发布时间】:2016-06-12 21:41:41
【问题描述】:

我正在尝试编译然后在 windows 中通过命令行运行程序。

我有以下代码,我尝试了我评论的线程方法,然后尝试了单进程方法。类文件被生成,希望执行也能产生 hello world。如何在字符串中捕获它。请注意,这是我使用的 Windows 机器。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class CompilerMain {

  public String doCompile(String compilationLang, String codeString,
                          String challengeName) throws InterruptedException, IOException {

    String output = null;
    String commandString = null;

    if (compilationLang == "java") {

      String path = "c:\\Test\\";

      challengeName = "Solution";
      String commandStringCompile;
      String commandStringExcecute;
      commandStringCompile = "javac " + path + challengeName + ".java";
      commandStringExcecute = "java " + path + challengeName;
      System.out.println("Command is " + commandStringCompile);
      System.out.println("Command is " + commandStringExcecute);
      final Process p = Runtime.getRuntime().exec(commandStringCompile);
      final Process q = Runtime.getRuntime().exec(commandStringExcecute);
      Scanner err = new Scanner(p.getInputStream());
      Scanner in = new Scanner(q.getInputStream());


      while (in.hasNext()) {

        System.out.println(in.next());

      }

      while (err.hasNext()) {

        System.out.println(err.next());

      }

      BufferedReader input = new BufferedReader(
          new InputStreamReader(p.getInputStream()));
      String line = null;

      try {
        while ((line = input.readLine()) != null) {
          System.out.println(line);
        }
      } catch (IOException e) {
        e.printStackTrace();
      }


        /*          
        new Thread(new Runnable() {
            public void run() {

                BufferedReader input = new BufferedReader(
                        new InputStreamReader(p.getInputStream()));
                String line = null;

                try {
                    while ((line = input.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        p.waitFor();
        if(p.exitValue()==0){
            System.out.println("Error! Code " + p.exitValue());
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = null;

            try {
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(p.exitValue()!=0){
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = null;

            try {
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        final Process q = Runtime.getRuntime().exec(commandString);
        commandString = "java "+ challengeName + " >>out.txt";
        new Thread(new Runnable() {
            public void run() {

                BufferedReader input = new BufferedReader(
                        new InputStreamReader(q.getInputStream()));
                String line = null;

                try {
                    while ((line = input.readLine()) != null) {
            //          System.out.println(" 1");

                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        q.waitFor();
        System.out.println("Exit Value is " + q.exitValue());
    */
    }
    return output;
  }
}

【问题讨论】:

    标签: java multithreading cmd console


    【解决方案1】:

    您的方法效果很好。可能是您的命令字符串不正确。请尝试从 p.getErrorStream()q.getErrorStream() 中读取,你会看到发生了什么。

    我稍微修改了你的代码,以便在我的 Linux 机器上工作。

    public String doCompile(String compilationLang, String codeString,
                            String challengeName) throws InterruptedException, IOException {
    
      String output = null;
      String commandString = null;
    
      if (compilationLang == "java") {
    
        String path = "/home/john/IdeaProjects/Test1/src/";
    
        String commandStringCompile;
        String commandStringExcecute;
        commandStringCompile = "javac " + path + "Main.java";
        commandStringExcecute = "java -cp " + path +" Main";
        System.out.println("Command is " + commandStringCompile);
        System.out.println("Command is " + commandStringExcecute);
        final Process p = Runtime.getRuntime().exec(commandStringCompile);
        final Process   q = Runtime.getRuntime().exec(commandStringExcecute);
        Scanner err = new Scanner(p.getErrorStream());
        Scanner in = new Scanner(p.getInputStream());
        while (in.hasNext()) {
          System.out.println(in.next());
        }
        while (err.hasNext()) {
          System.out.println(err.next());
        }
    
        err = new Scanner(q.getErrorStream());
        in = new Scanner(q.getInputStream());
        while (in.hasNext()) {
          System.out.println(in.next());
        }
        while (err.hasNext()) {
          System.out.println(err.next());
        }
      }
      return output;
    }
    

    输出:

    Command is javac /home/john/IdeaProjects/Test1/src/Main.java
    Command is java -cp /home/john/IdeaProjects/Test1/src/ Main
    hello
    world!
    

    【讨论】:

    • 我无法验证,我可以看到错误流,但看不到输出流,也许这是 Windows 的问题。让我尝试在 linux 平台上运行它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2020-11-02
    • 2013-05-21
    • 1970-01-01
    相关资源
    最近更新 更多