I am using JavaCompiler to compile Java code at runtime. For most part, it works well. There are no exceptions during compiling, it works well. I require it to compile a class with @Entity annotation. However, I keep getting "Error in line 3 - class, interface, or enum expected".
This is my entity class:
@Entity
public class Contact {
@Getter @Setter
private String FirstName;
@Getter @Setter
private String LastName;
}
This is how I am using JavaCompiler to compile it:
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path"));
Iterable<? extends JavaFileObject> compilationUnit
= fileManager.getJavaFileObjectsFromFiles(Arrays.asList(newClassFile));
JavaCompiler.CompilationTask task = compiler.getTask(
null,
fileManager,
diagnostics,
optionList,
null,
compilationUnit);