0

I need to dynamically compile a class, which uses certain imports. To compile it I use the JavaCompiler class:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, classPath);

I am trying to compile a class with these kind of imports:

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "Customer")
public class CustomerEntity {

  private String firstname;
}

Upon compiling I do get the following error:

core\src\main\java\com\example\application\cobigenexample\customermanagement\dataaccess\api\CustomerEntity.java:3: error: package javax.persistence does not exist
import javax.persistence.Entity;
                        ^
core\src\main\java\com\example\application\cobigenexample\customermanagement\dataaccess\api\CustomerEntity.java:4: error: package javax.persistence does not exist
import javax.persistence.Table;
                        ^
core\src\main\java\com\example\application\cobigenexample\customermanagement\dataaccess\api\CustomerEntity.java:11: error: cannot find symbol
@Entity
 ^
  symbol: class Entity
core\src\main\java\com\example\application\cobigenexample\customermanagement\dataaccess\api\CustomerEntity.java:12: error: cannot find symbol
@Table(name = "Customer")
 ^
  symbol: class Table
4 errors

How can I compile the class, so that the compiler finds the needed packages?

sigur
  • 662
  • 6
  • 21
Zylesto
  • 1
  • 4

0 Answers0