When compiled, Diagnostic issues a package org.apache.log4j does not exist
I have here such method which in rantaym should compile a source code java.
public static void compile(final File file, final JavaFileObject... objects) { System.setProperty("java.home", "c:\\Program Files\\Java\\jdk1.7.0_45"); final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); final Iterable<? extends JavaFileObject> compilationUnits = new ArrayList<JavaFileObject>(Arrays.asList(objects)); String[] compileOptions = new String[] { "-d", file.getAbsolutePath() }; final Iterable compilationOptions = Arrays.asList(compileOptions); final JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, compilationOptions, null, new ArrayList(Arrays.asList(objects))); final boolean result = task.call(); if (result) { System.out.println("Compilation was successful"); } else { System.out.println("Compilation failed"); for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { System.err.format("Error on line %d in %s", diagnostic.getLineNumber(), diagnostic); } } try { fileManager.close(); } catch (final IOException e) { e.printStackTrace(); } } In File, the path is D: \ project \ src \ main \ resources In JavaFileObject, for example, comes the following source:
import java.lang.Override; import java.lang.String; import org.apache.log4j.Logger; public class Audi implements Car { private String engine; private final Logger LOGGER = Logger.getLogger(this.getClass()); @Override public void start() { LOGGER.info("The engine starts"); setEngine("start"); } @Override public void stop() { LOGGER.info("The engine stops"); setEngine("stop"); } public String getEngine() { return this.engine; } public void setEngine(final String engine) { this.engine = engine; } } If you remove the Logger, then everything compiles fine. How to deal with this please tell me?
Best wishes!