Note that there are some explanatory texts on larger screens.

plurals
  1. POCompile java files programmatically
    primarykey
    data
    text
    <p>I know this has been asked and answered a lot but I still don't have a good solution and I still don't understand some parts. So I have the requirement to compile *.java files programmatically.</p> <pre><code>JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); </code></pre> <p>is what I'm using and (as expected) the compiler is <code>null</code>. Now, I do know that I have to use the JDK and not the JRE as a "runtime", but here is something I don't understand: isn't it enough to just put <code>tools.jar</code> in the classpath of the application and then have access to the Java Compiler API? If this is true, is there (I think there is) difference between a stand-alone java application and web-based application. Actually, I am trying to summon the JavaCompiler from a PlayFramework webapp so I figured out, maybe this solutions (with including the <code>tools.jar</code>) only works for stand-alone applications?</p> <p>I also tried to create a custom ClassLoader and invoke the above mentioned method with reflection but all I am getting is <code>null</code> for the <code>compiler</code> object:</p> <pre><code>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&lt;?&gt; loadClass = newCL.loadClass("javax.tools.ToolProvider"); Method method = loadClass.getMethod("getSystemJavaCompiler", null); Object object = method.invoke(null); System.out.println("Object: " + object); // NULL </code></pre> <p>Explanation for the code above:</p> <ul> <li>I haven't included try/catches for the sake of simplicity.</li> <li>app.classloader() is a Play-method which returns the ClassLoader of the App</li> <li><code>tools.jar</code> is included in my lib folder of the Play project (this implies that it is on the project's classpath - according to the Play documentation)</li> </ul> <p>I am pretty sure there is something else I need to do before Play is able to load the Java Compiler class I just don't know what I am missing.</p> <p>Options like <code>Runtime.exec("javac myFile.java")</code> and the Eclipse JDT compiler are known to me, but that's not what I am looking for.</p> <p>Oh, and something like <code>System.setProperty("java.home", "PATH_TO_YOUR_JDK");</code> and then <code>ToolProvider.getSystemJavaCompiler();</code> is working but I find this solution so ugly.</p> <p>Best regards</p> <p><strong>EDIT</strong>: (to give additional information and reflect the last status) This is a basic representation of web-app-structure:</p> <pre><code>myApp |-conf\... |-lib\MyJar.jar |-lib\tools.jar |-logs\... |-... </code></pre> <p>MyJar.jar has now a <code>META-INF/MANIFEST.MF</code> file with the following content:</p> <pre><code>Manifest-Version: 1.0 Sealed: true Main-Class: here.comes.my.main.class Class-Path: tools.jar </code></pre> <p><em>Without</em> starting the Play app, I am trying (in the <code>lib</code> folder): <code>java -jar MyJar.jar</code> - my simple <code>main</code> method tries to invoke the Compiler (<code>ToolProvider.getSystemJavaCompiler();</code>) and returns <code>null</code>. So this leads me to believe, that the problem has nothing to do with Play - I can't even get a Compiler when normally running my Jar!</p>
    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.
    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