A bug in JDK 1.8 Server VM using in memory compile
Prakash Oraon
poraon at yahoo.com
Wed Jun 1 13:44:19 UTC 2016
I am compiling a java source (stored in String variable) and using JavaCompiler / CompilationTask to compile the source in memory using JDK 1.8, source compatibility set to 1.7I have the following classes - DynamicCompiler - The main driver program. A, B, C - These classes are refereed from the source. B implements C. Class A, B and C are compiled in three differnt JARS - A.jar, B.jar and C.jar located in C:/Temp/testJpublic class DynamicCompiler {
public static void main(String[] args) {
String sourceCode =
"public class TestMain {\n"+
" public static void main(String[] args) throws NullPointerException {\n"+
"\n"+
" A a = new A();\n"+
"\n"+
" System.out.println(\"Inside Test Main...\");\n"+
" a.getB().printB();\n"+
" }\n"+
"\n"+
"}";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
Iterable<String> options = Arrays.asList(new String[] { "-classpath", "C:/Temp/testJ/A.jar;C:/Temp/testJ/B.jar;","-source","1.7"});
JavaFileManager fileManager =
new ClassFileManager(compiler.getStandardFileManager(null, null, null));
DiagnosticCollector<JavaFileObject> diagnosticListener = new DiagnosticCollector<JavaFileObject>();
List<JavaFileObject> jfiles = new ArrayList<JavaFileObject>();
jfiles.add(new CharSequenceJavaFileObject("TestMain", sourceCode));
CompilationTask complieTask = compiler.getTask(null, fileManager, diagnosticListener, options, null, jfiles);
if(!complieTask.call()) {
StringBuffer bufferError = new StringBuffer();
List<Diagnostic<? extends JavaFileObject>> compliationErros = diagnosticListener.getDiagnostics();
for (Diagnostic<? extends JavaFileObject> diagnostic : compliationErros) {
bufferError.append(diagnostic);
}
System.out.printf("Errors:%s",bufferError.toString());
} else {
System.out.println("Compilation Successfull...");
}
}public class A {
private B b;
A() {
b = new B("A");
}
public void printA() {
System.out.println("Inside A...");
b.printB();
}
public B getB() {
return b;
}public class B implements C {
private String name;
B(String name) {
this.name = name;
}
public void printB() {
System.out.println("Inside B...");
System.out.println(name);
}
@Override
public void printC() {
System.out.println("Inside B.printC");
}public interface C {
void printC();
}The source (stored in variable sourceCode in class DynamicCompiler) is getting compiled using - java version "1.8.0_11" Java(TM) SE Runtime Environment (build 1.8.0_11-b12) Java HotSpot(TM) Client VM (build 25.11-b03, mixed mode)
But the same source (stored in variable sourceCode in class DynamicCompiler) is not getting compiled using - java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b18) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)I am getting the following error -Errors:/TestMain.java:7: error: cannot access C a.getB().printB(); ^ class file for C not foundIf I add C:/Temp/testJ/C.jar in the classpath as below, the source is compiled.Iterable options = Arrays.asList(new String[] { "-classpath", "C:/Temp/testJ/A.jar;C:/Temp/testJ/B.jar;C:/Temp/testJ/A.jar","-source","1.6"});Question -Why does the same code gets compiled in JDK 1.8 64-Bit Server VM but not in JDK 1.8 Client VM.? Is there a difference in compilation model between Client and Server VM in JDK 1.8?Thanks, Prakash
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20160601/d3d2175f/attachment.html>
More information about the compiler-dev
mailing list