0

I have read that Java 6 has a compiler api which allows you to compile java files from other java code. However whenever I try to import any such class (eg javax.tools.JavaCompilerTool), I get a

unable to resolve class javax.tools.JavaCompilerTool

I searched in all the jar files in the jdk1.6.0_26 installation directory and found no reference to JavaCompilerTool in any jar file in the package javax.tools.

My understanding is that this functionality is in the 1.6 JDK. I am running my application with the java.exe from the \bin directory and not the \jre\bin directory but regardless neither work.

What am I missing? How can the class not be found.

Coder
  • 1,375
  • 2
  • 20
  • 45
  • Is that a compiletime or runtime error? (I don't recognize it being a compiletime one from Oracle). If runtime, how exactly are you running it? As a webapp on a servletcontainer or something? If so, are you sure the servletcontainer is using the JDK's JRE? – BalusC Aug 03 '11 at 17:09
  • I am running it from command line (a .bat file under windows 7 64 bit). The error is a runtime error. I start a java app which then starts a GroovyScriptEngine, which runs a groovy file, which then tries to import the JavaCompilerTool. I am sure I am using the JDK's java.exe, however if from the groovy script i print System.getProperty("java.home") it points to the \jre\bin directory. I'm not sure if this is a reliable check though. – Coder Aug 03 '11 at 17:18
  • If i instead put the import in the main java file (before groovy is even started, i get a "cannot find symbol" on import javax.tools.JavaCompilerTool;. So the same issue – Coder Aug 03 '11 at 17:20
  • See my reply to the answer below. Are you sure that path has to reflect the jdk\bin directory? It seems to work without it being reflected and i'm sure i'm pointing to the jdk\bin\java.exe file as i'm running from a .bat file – Coder Aug 03 '11 at 17:24

2 Answers2

2

You need to use the javax.tools.JavaCompiler class. Take a look at the javadocs for usage.

Example:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File("HelloWorld.java")));
compiler.getTask(null, fileManager, null, null, null, compilationUnits).call();
fileManager.close();
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • I haven't had a chance to fully try this solution, but adding imports for import javax.tools.JavaCompiler; import javax.tools.ToolProvider; import javax.tools.StandardJavaFileManager; don't break at runtime so all the examples i found on google must have been wrong. I will need to try your code when I get a chance but it looks promising. Thanks. – Coder Aug 03 '11 at 17:22
  • Look around on Javadocs or [Stackoverflow.com](http://www.google.com/search?q=toolprovider.getsystemjavacompiler+site%3Astackoverflow.com) instead :) See for example http://stackoverflow.com/questions/2946338/how-do-i-programmatically-compile-and-instantiate-a-java-class – BalusC Aug 03 '11 at 17:26
  • Ok i tried your code and it produced a .class file from my Java file. I call this a success. Thanks for the help. – Coder Aug 03 '11 at 17:31
0

I see javax.tools.JavaCompiler in my JDK 6 rt.jar. Perhaps you have the incorrect class name.

duffymo
  • 305,152
  • 44
  • 369
  • 561