简体   繁体   中英

Ant Javac Compilation Output

I am trying to compile java files from a java program using org.apache.tools.ant.taskdefs.Javac and to print its output on screen.

See the below code snippet for your reference,

Javac javaCompile = (Javac) webServiceProject.createTask("javac");
javaCompile.setSrcdir("D:\\Java\\src");
javaCompile.setDestdir("D:\\Java\\classes");
try{
    javaCompile.execute();
}catch (BuildException buildException){
    FacesMessage message = new FacesMessage(buildException.getMessage());
    message.setSeverity(FacesMessage.SEVERITY_FATAL);
    FacesContext.getCurrentInstance().addMessage(null, message);
}

When i compile the files with the above code and if any compilation error exists, i get a message "Compile failed; see the compiler error output for details." .

I don't know how to retrieve the compilation error and show it as an output on screen. Can anyone suggest how to retrieve it?

The below code did the trick and this retrieved me the compiler error.

Javac javaCompile = (Javac) webServiceProject.createTask("javac");
javaCompile.createCompilerArg().setValue("-Xstdout");
String filePath = basePath + "resources" + File.separator
        + "errorlog.log";
javaCompile.createCompilerArg().setFile(new File(filePath));
javaCompile.setNowarn(true);

Ignoring the issue of whether you should be compiling code in a JSF application ...

I believe that Ant sends compilation output to the logger. So to get hold of it, you will need to configure the logger to send the relevant log events to a file ... or something.

Try changing the build.compiler setting. Ant was having trouble finding the compiler I had specified, "org.eclipse.jdt.core.JDTCompilerAdapter" - changing it to "modern" allowed me to compile.

This was tricky to track down. I'm not sure why there was no output.

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