0

In my idea IDE, I can see the compile error with red font in the console.But when I deploy the jar in the linux server.I can not see the compile log.How to print the compile error log?

public static void main(String[] args) throws Exception { 
        String compliePath="D:\\testFole";
        String filename="D:\\test.java";
        String[]  arg = new String[] { "-d", compliePath,  filename };
        System.out.println(com.sun.tools.javac.Main.compile(arg));
    } 

enter image description here

1 Answer 1

1

Well if I got your question right, here is an approach to the outcome. I think this will be platform-independent.

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    
    private static Process process;

    public static void main(String[] args) {
        
        runCommand();
        getErrorMessage();

    }
        

    /**
     * This method executes/runs the commands
     */
    private static void runCommand()
    {
        File file = new File("D:\\\\test.java");
        
        String changeDirectory = "cmd start cmd.exe /c cd D:\\";
        String compile = " && javac D:\\test.java";
        String run = " && java "+file.getName().replace(".java","");
        String command = changeDirectory + compile + run;

        try {
               process = Runtime.getRuntime().exec(command);
        }catch (IOException e){}
    }
        
    

    /**
     * This method will get the errorStream from process
     * and output it on the console.
     */
    private static void getErrorMessage()
    {  
     
         try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())))
         {
             String line;
             
             if(errorReader.readLine() != null)
                while ((line = errorReader.readLine()) != null)
                    System.out.println(line);         //display error message
       
         }catch (IOException e){}
     }

}
Sign up to request clarification or add additional context in comments.

1 Comment

If I use com.sun.tools.javac.Main.compile() method ,can I print the compile log in the linux server when deploy the jar?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.