1

I need to compile java files which have dependencies that only exist as class files which have been loaded by the JVM doing the compilation. I am using the javax.tool JavaCompiler somewhat as described here: How do I programmatically compile and instantiate a Java class?

Additionally, I am using a StandardJavaFileManager to specify to the compiler my local classpath and the java files I am compiling.

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
...
compiler.getTask(null, fileManager, null, null, null, compilationUnits2).call();

The the dependencies that are required have been loaded by a custom system class loader, and for security reasons cannot be written to disc to be supplied to the compiler's classpath.

Is there a way to get the JavaCompiler returned by ToolProvider.getSystemJavaCompiler() from the standard oracle JDK to use the class files which have been loaded by my custom class loader without writing them to disc? I'm also open to extending JavaCompiler or JavaFileManager as needed.

The only option I have come up with so far would be to use reflection to create dependencies as stubbed java files on disc, compile them, and provide them to the JavaCompiler. I don't care for this option as it seems cumbersome, and difficult as the dependencies are quite complex.

I've seen these questions:

Compile code fully in memory with javax.tools.JavaCompiler

How do I use JDK6 ToolProvider and JavaCompiler with the context classloader?

However, these solutions handle dynamic compilation, but do not seem to address the memory only dependency issue.

Community
  • 1
  • 1
Skcussm
  • 658
  • 1
  • 5
  • 20
  • You could use a non-standard file manager...? – user253751 Jun 14 '16 at 22:39
  • Sure, if that is a solution. Too often I have found libs in the jdk are overly dependent on getting down to the File object. Will that work for the javacompiler? – Skcussm Jun 15 '16 at 02:48
  • Possible duplicate of [Compile code fully in memory with javax.tools.JavaCompiler](http://stackoverflow.com/questions/12173294/compile-code-fully-in-memory-with-javax-tools-javacompiler) – Paul Sweatte Jan 20 '17 at 19:01
  • @Skcussm Yes, it will work. https://github.com/michaelliao/compiler/blob/master/src/main/java/com/itranswarp/compiler/MemoryJavaFileManager.java What I'm wondering is what URI you use to reference files in this filesystem... – Jonathan Locke Dec 22 '22 at 17:52
  • @JonathanLocke 'file:///C:/java/SomeClass.java' for example. – Skcussm Mar 23 '23 at 17:18

0 Answers0