Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing JavaCompiler in an OSGi Bundle
    text
    copied!<p>I'm in the process of refactoring a Java application to use OSGi. One feature of the application is on-the-fly Java compilation using <code>javax.tools.JavaCompiler</code>. In the original application this process worked by feeding the compiler the existing classpath, like so. </p> <pre><code>JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); String[] options = {"-classpath", System.getProperty("java.class.path")}; DiagnosticListener&lt;JavaFileObject&gt; listener = new DiagnosticListener&lt;JavaFileObject&gt;() {...}; StandardJavaFileManager fileManager = compiler.getStandardFileManager(listener, null, null); Iterable&lt;? extends JavaFileObject&gt; fileObjects = fileManager.getFileObjects(sourceFile); CompilationTask task = compiler.getTask(null, fileManager, listener, Arrays.asList(options), null, fileObjects); task.call(); </code></pre> <p>However, this won't work in an OSGi bundle since the classpath no longer contains the needed paths. In the refactored OSGi version of the application, the compiler needs access to classes that are within the same bundle as the above code, as well as classes from other bundles. How do I make the compiler aware of these classes?</p> <p>I've thought of two possible solutions:</p> <ol> <li>Give the compiler the classloader used by the bundle containing the above code since it is aware of all the necessary classes. However, this doesn't seem like a viable solution from what I've read <a href="https://stackoverflow.com/questions/2315719/using-javax-tools-toolprovider-from-a-custom-classloader">here</a> and <a href="https://stackoverflow.com/questions/274474/how-do-i-use-jdk6-toolprovider-and-javacompiler-with-the-context-classloader">here</a>.</li> <li>Build the classpath using the physical locations of the installed bundles. I've looked at <code>org.osgi.framework.Bundle.getLocation()</code> but I'm not sure if this would be a reliable solution. The paths I get back (at least when deploying within Eclipse) are relative and I'm not sure if they'd be safe to use across all platforms and situations.</li> </ol> <p>Does option two above seem possible? Is there a better solution?</p>
 

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