Friday, June 8, 2012

Playing around to Compiling Java Files At Runtime using Compiler API, as a result of JSR 199.

I was playing around to compiling java files at runtime using Compiler API as a result of JSR 199.  I used source at Compile Java Files At Runtime. From the source "ToolProvider.getSystemJavaCompiler()" at line 28 was returning null. Posting here so it might help someone else facing same issue in future.

I was getting java.lang.NullPointerException so I did "System.out.println(System.getProperty("java.home"));" and noticed my eclipse was pointing "C:\Program Files\Java\jre7" even after I changed my preferences to use JDK1.7 in place of JRE1.7. This error accors if java development envrionment points to JRE instead of JDK as it is menioned in a bug report at link:

I found a work around by forcing to use JDK1.7 by setting system property like this: "System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_02");"

Then I compiled my program and did not get any NullPointerException.