Compiling java files programmatically
I know this has been asked a lot, but I still don't have a good solution. I still don't understand some parts So I need to compile it programmatically * Java file
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
The compiler I used (and expected) is empty Now, I know I have to use JDK instead of JRE as the "runtime", but here is what I don't understand: just put tools Jar is placed in the classpath of the application. Can I access the java compiler API? If this is true, is there (I think there) a difference between stand - alone Java applications and Web - based applications In fact, I tried to transfer out java compiler from the playframework webapp, so I figured out that maybe this solution (including tools. Jar) is only applicable to stand-alone applications?
I also try to create a custom classloader and use reflection to call the methods mentioned above, but all the compiler objects I get are empty:
ClassLoader classloader = app.classloader(); File file = new File("lib/tools.jar"); URL url = file.toURI().toURL(); URL[] urls = new URL[]{url}; ClassLoader newCL = new urlclassloader(urls,classloader) { }; Class<?> loadClass = newCL.loadClass("javax.tools.ToolProvider"); Method method = loadClass.getmethod("getSystemJavaCompiler",null); Object object = method.invoke(null); System.out.println("Object: " + object); // NULL
The above code Description:
>For simplicity, I didn't include try / catch. > app. Classloader () is a playback method that returns classloader > tools of app Jar is contained in the Lib folder of my play project (which means it's on the classpath of the project – according to the play documentation)
I'm sure there are other things I need to do before play can load java compiler classes. I just don't know what I'm missing
Like runtime Options such as exec ("javac myfile. Java") and the eclipse JDT compiler are known to me, but this is not what I am looking for
Oh, like system Setproperty ("Java. Home", "path_to_your_jdk"); Then toolprovider getSystemJavaCompiler(); Working, but I found the solution so ugly
Best wishes
Edit: (provide more information and reflect the final state) this is the basic representation of the web application structure:
myApp |-conf\... |-lib\MyJar.jar |-lib\tools.jar |-logs\... |-...
MyJar. Jar now has a meta - inf / manifest MF file, as follows:
Manifest-Version: 1.0 Sealed: true Main-Class: here.comes.my.main.class Class-Path: tools.jar
Without starting the play application, I'm trying (in the Lib folder): Java - jar myjar Jar - my simple main method attempts to call the compiler (toolprovider. Getsystemjavacompiler();) And returns null So it makes me believe that this problem has nothing to do with Playback - I can't even get a compiler when my jar runs normally!
Solution
There is no difference between a web application and a standalone application
You should not use tools Jar is packaged into your webapp classpath Class loaders in Java usually work only from the bottom Therefore, the toolprovider that is part of the JDK will not see your tools in the webapp classpath Jar (it can't see the webapp classpath down)
Solution: using JDK, make sure to point to or place tools Jar in Java ext dir You can set the attribute - DJava Set ext.dir to any directory you like to override ext dir