1

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). 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. 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. 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?

Alfred Young
  • 397
  • 1
  • 3
  • 15
  • 1
    Here's one suggestion that has nothing to do with your actual question: Don't be so broad! Don't ask for generic suggestions, ask a _specific question_ that we can answer: Something like "How can I run the compiled file and capture the output?" (which, incidentally, I think has already been covered here) – Nic May 25 '15 at 01:16
  • 1
    Anyway, a couple of related (but _not duplicate_) things that you may find helpful: [Using a threat to capture process output](http://stackoverflow.com//questions/1670798/using-a-thread-to-capture-process-output), [the Oracle tutorial on running a file from the command line](https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html#win32-2c), and [Execute command line program](http://stackoverflow.com/questions/9346077/java-execute-command-line-program). – Nic May 25 '15 at 01:18

1 Answers1

2

Once it's compiled, you will need to load the Class object and then invoke the main(String[]) method. To capture the stdout, you will need to use 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)));
    }
}
Yosef Weiner
  • 5,432
  • 1
  • 24
  • 37