简体   繁体   中英

How to get the Java compiler error messages when calling it as an external process?

I want to make my own small java editor, but I don't know, whats the best way to compile a java class, and get the return messages like errors.

I tryed it so, but I can not get further...

    ProcessBuilder builder = new ProcessBuilder("javac");
    try
    {
        Process process = builder.start();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

You can use javac pointing to the java file which will generate one .class file, then you have to tart another process to run the class file (if it has a main method) and consume it's output to show it on your editor.

You might also use Eclipse Compiler for Java which is a jar file that gives you an API for compiling on the fly java What is the difference between javac and the Eclipse compiler?

But why are you doing this in the first place ?

Process documentation offers getErrorStream() and getOutputStream() methods. Read that streams, but from separate threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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