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!

  • not enough org.apache.log4j library in classpath - Senior Pomidor
  • It is interesting that in klasspas it. In the source code Audi.java eclips does not swear. But when it is a compilation in runtime, this situation turns out ... - Dron4K

1 answer 1

I corrected your code a bit and compiled and executed without errors. I checked on the command line.

 public static void compile(final File file) { final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); List<String> optionList = new ArrayList<>(); optionList.add("-classpath"); optionList.add(System.getProperty("java.class.path") + System.getProperty("path.separator") + "/log4j-1.2.17.jar"); Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(file)); JavaCompiler.CompilationTask task = compiler.getTask( null, fileManager, diagnostics, optionList, null, compilationUnit); 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(); } } 
  • Added by. Such request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String - Dron4K
  • @ Dron4K updated the answer - Mikhail Vaysman