简体   繁体   English

从Java代码中运行已编译的Java .class文件并捕获输出

[英]Running a compiled java .class file from within java code and capturing output

I'm trying to write a java program with an interface that allows the user to create .java file and compile and run it (essentially a very simplistic IDE). 我正在尝试编写一个带有接口的Java程序,该接口允许用户创建.java文件并进行编译和运行(本质上是非常简单的IDE)。 I'm using java swing for the gui and have so far been able to compile a .java file from within the interface into a .class file. 我正在使用Java swing作为gui,到目前为止,已经能够将接口中的.java文件编译为.class文件。 I've been researching how to run a .class file from within java code but have found a wide range of answers which I haven't been able to get working. 我一直在研究如何从Java代码中运行.class文件,但发现了很多无法解决的答案。 Here is the relevant code for compilation: 以下是相关的编译代码:

File javaFile = new File( "test1.java" );
String code = entry.getText(); // get text entered by user in GUI
try{
  PrintWriter writer = new PrintWriter( javaFile );  // write text to .java file  
  writer.print( code );
  writer.close();
}
catch( FileNotFoundException e ){
  System.err.println( e );
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
List<File> sourceFileList = new ArrayList<File>();
sourceFileList.add( javaFile );
StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
Iterable<? extends JavaFileObject> javaSource = fileManager.getJavaFileObjectsFromFiles( sourceFileList );
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, javaSource);
task.call(); // compile .java file into .class file

How can I run a compiled .class file and capture its output within my code? 如何运行已编译的.class文件并在代码中捕获其输出?

Once it's compiled, you will need to load the Class object and then invoke the main(String[]) method. 编译之后,您将需要加载Class对象,然后调用main(String[])方法。 To capture the stdout, you will need to use System.setOut . 要捕获标准输出,您将需要使用System.setOut

private String invokeClass(String className) throws URISyntaxException, IOException, ReflectiveOperationException {
    Class<?> clazz = Class.forName(className);
    // Alternatively, you can load the new class with a new Classloader, if you don't want to pollute the current Classloader
    // Class<?> clazz = new URLClassLoader(new URL[]{getClass().getClassLoader().getResource("").toURI().toURL()}, getClass().getClassLoader()).loadClass(className);
    Method main = clazz.getDeclaredMethod("main", String[].class);
    try ( ByteArrayOutputStream out = new ByteArrayOutputStream();
          PrintStream ps = new PrintStream(out)) {
        System.setOut(ps);
        main.invoke(main, new Object[]{new String[0]});
        return out.toString();
    }
    finally {
        // Reset to the console
        System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM