java - 如何获取java程序的输出?

标签 java compiler-construction

我需要编写一段代码来帮助我获取程序的输出。

我正在编译的 Java 文件 MainClass.java 中有一个打印 "OK" 的简单代码。

如何返回这个输出,即使有错误,我也需要接受。

这是我编译和创建.class 文件的代码。

File sourceFile = new File("C:\\Projet_Interne\\webProject\\NewFolder\\MainClass.java");  
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null,sourceFile.getPath());
int result = compiler.run(System.in,System.out,System.err,sourceFile.getPath());
     System.out.println("Compile result code = " + result);
      if(compilationResult == 0){
            System.out.println("Compilation is successful");
      }else{
             System.out.println("Compilation Failed");
      }
 StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);
 stdFileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("C:\\Projet_Interne\\webProject\\NewFolder")));

最佳答案

您可以使用DiagnosticCollector,它将允许您收集有关编译过程的诊断信息,您可以从JavaDocs 中找到更多详细信息。

例如……

File helloWorldJava = new File(...);

DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);

// This sets up the class path that the compiler will use.
// I've added the .jar file that contains the DoStuff interface within in it...
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path"));

Iterable<? extends JavaFileObject> compilationUnit
        = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(helloWorldJava));
JavaCompiler.CompilationTask task = compiler.getTask(
    null, 
    fileManager, 
    diagnostics, 
    optionList, 
    null, 
    compilationUnit);
if (task.call()) {
    System.out.println("Yipe");
} else {
    // Opps compile failed...
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
        System.out.format("Error on line %d in %s%n",
                diagnostic.getLineNumber(),
                diagnostic.getSource().toUri());
    }
}
fileManager.close();

编译后,您有两个选择,您可以使用自定义类加载器加载类并执行它,这将使用您当前的标准输出...

// Create a new custom class loader, pointing to the directory that contains the compiled
// classes, this should point to the top of the package structure!
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("./").toURI().toURL()});
// Load the class from the classloader by name....
Class<?> loadedClass = classLoader.loadClass("testcompile.HelloWorld");
// Create a new instance...
Object obj = loadedClass.newInstance();
// Santity check
if (obj instanceof DoStuff) {
    // Cast to the DoStuff interface
    DoStuff stuffToDo = (DoStuff)obj;
    // Run it baby
    stuffToDo.doStuff();
}

或者使用ProcessBuilder来执行另一个Java进程...

ProcessBuilder pb = new ProcessBuilder("java", "HelloWorld");
pb.directory(new File("src"));
pb.redirectError();
Process p = pb.start();

InputStreamConsumer.consume(p.getInputStream());

p.waitFor();

作为引用,InputStreamConsumer....

public static class InputStreamConsumer implements Runnable {

    private InputStream is;

    public InputStreamConsumer(InputStream is) {
        this.is = is;
    }

    public InputStream getInputStream() {
        return is;
    }

    public static void consume(InputStream is) {
        InputStreamConsumer consumer = new InputStreamConsumer(is);
        Thread t = new Thread(consumer);
        t.start();
    }

    @Override
    public void run() {
        InputStream is = getInputStream();
        int in = -1;
        try {
            while ((in = is.read()) != -1) {
                System.out.print((char)in);
            }
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }

}

这在以下内容中有更详细的概述:

关于java - 如何获取java程序的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25177372/

相关文章:

c# - C++ 运算符重载

linux - 如何在Linux上编写PIC Controller 程序?

java - IntelliJ : How to make Maven project use only the dependencies show in pom. xml 文件

java - 如何等待 java 声音剪辑完成播放?

parsing - 在创建抽象语法树的上下文中,综合属性是什么?

compiler-construction - 为运行字节码编写 VM 的源代码

compiler-construction - JIT 编译是如何在运行时实际执行机器码的?

java - JAX-RS 资源方法可以返回多个图像吗?

java实时优先级高于应有的优先级

java - 使用单键改造发送多个图像