Search code examples
javajakarta-eejpajava-compiler-api

Java EE server independent way to locate JAR containing javax.persistence classes


I have a Java EE application where you can define variables of a certain type. To validate that the value expression is valid for it's type, I create a string containing a small class:

public class CompilableExpression {
  private <type> expression = <expression>;
}

.. and try to compile it using JavaCompiler:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
Iterable<? extends JavaFileObject> compilationUnits = 
  Arrays.asList(stringContainingCompilableExpression);
CompilationTask task = compiler.getTask(
  null, null, diagnostics, options, null, compilationUnits
);
task.call();

This works fine if you are using type: String and expression: "my string", or type: Integer and expression: 10.

I also want to validate types using the @Entity annotation.

When I try to do so I get an error:

Cannot find annotation method name() in type javax.persistence.Table: class file for javax.persistence.Table not found

So, I need to add a JAR containing javax.persistence classes to the class path somehow. Is there a generic way to find this JAR? I'm using GlassFish, and don't want to build a GlassFish only solution.

Or is adding it to my project as a normal (non provided) dependency the way to go?

Update

I'm trying to at least find the location in GlassFish (at ~/glassfish-4.1/glassfish):

find ./ -name '*ee*.jar'          
./lib/javaee.jar
./modules/security-ee.jar
./modules/amx-javaee.jar
./modules/javaee-kernel.jar
./modules/autostart/osgi-javaee-base.jar
./modules/autostart/osgi-ee-resources.jar
./modules/deployment-javaee-full.jar
./modules/deployment-javaee-core.jar
./modules/glassfish-ee-api.jar
./modules/javax.management.j2ee-api.jar

My best guess is to use ./lib/javaee.jar, but when I check the contents it's almost empty:

$JAVA_HOME/bin/jar tf ./lib/javaee.jar
META-INF/
META-INF/MANIFEST.MF
META-INF/maven/
META-INF/maven/org.glassfish.main.extras/
META-INF/maven/org.glassfish.main.extras/javaee/
META-INF/maven/org.glassfish.main.extras/javaee/pom.xml
META-INF/maven/org.glassfish.main.extras/javaee/pom.properties

Does anyone know where (in the GlassFish installation) to get the JAR including the javax.persistence classes?


Solution

  • The JAR you are looking for is in $GLASSFISH_HOME/glassfish/modules/javax.persistence.jar

    If you are deploying to a JavaEE App Server, the JAR with the @Entity annotation will already be in your application's runtime classpath. You shouldn't have to load any JAR files in code (as you described in your comment).

    During development you typically configure your App Server in your IDE and that process should include the JAR with the annotation into your compilation classpath.

    You might need to manually include it in the project compile classpath / application server libraries classpath depending on how your IDE handles this. For Glassfish all the API JARs are where you were looking in the modules directory.

    Even though this ties your project to finding the JAR for compilation in a specific location relative to the app server install I find it's still a better approach then copying JARs into you project for compilation. This ensures you are compiling against the correct JARs that are deployed to the app server and so long as these are JavaEE APIs your application will deploy fine into any app server.

    You could also set up your project to use Maven, include the required deps for the persistence APIs and it will find the compile time deps in your maven cache.

    Also you might want to check out Jar Explorer which lets you search for classes etc inside JARs, folders of JARs etc. Its pretty convenient for finding these things.