0

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);
Vicrum
  • 1
  • 2
  • 1
    Please read [mcve] and enhance your question accordingly. As in: add an example class that fails compilation. – GhostCat May 04 '19 at 17:30
  • Possible duplicate of [Do annotations in Java result in compile-time transitive dependencies?](https://stackoverflow.com/questions/29260270/do-annotations-in-java-result-in-compile-time-transitive-dependencies) – Dupinder Singh May 04 '19 at 18:06

1 Answers1

0

Solved. Looks like I just had to add the persistence api jar while building.

Vicrum
  • 1
  • 2