Note that there are some explanatory texts on larger screens.

plurals
  1. POJava compiler API ClassLoader
    primarykey
    data
    text
    <p>I am trying to use Java Compiler API to compile some java class. That class imports some packages from the jar files which can be loaded by context ClassLoader, let's call him X, which is NOT the system classloader. When I run the compilation, the compiler complains about not recognizing the imports. I have tried to specify the fileManager to pass the classloader, but it does not help. </p> <p>When compile method is called, it first prints "CLASS LOADED", so the context ClassLoader CAN find the dependency class. However, the compilation itself fails (I get "Compilation FAILED" message) and during the compilation I get errors like this:</p> <p>/path/to/my/Source.java:3: package my.dependency does not exist import my.dependency.MyClass; ^</p> <p>What am I doing wrong? What's the correct way to pass custom classloader to the compilationTask? I can't extract the URLs from the ClassLoader since it's not URLClassLoader.</p> <p>My methods are here:</p> <pre><code>public void compile(List&lt;File&gt; filesToCompile) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null); Iterable&lt;? extends JavaFileObject&gt; fileObjects = stdFileManager .getJavaFileObjectsFromFiles(filesToCompile); FileManagerImpl fileManager = new FileManagerImpl(stdFileManager); CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects); Boolean result = task.call(); if (result == true) { System.out.println("Compilation has succeeded"); } else { System.out.println("Compilation FAILED"); } } private final class FileManagerImpl extends ForwardingJavaFileManager&lt;JavaFileManager&gt; { public FileManagerImpl(JavaFileManager fileManager) { super(fileManager); } @Override public ClassLoader getClassLoader(JavaFileManager.Location location) { ClassLoader def = getContextClassLoader(); try { def.loadClass("my.dependency.MyClass"); System.out.println("CLASS LOADED"); } catch (ClassNotFoundException ex) { System.out.println("NOT LOADED"); } return def; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload