2

I get the following exception

ClassNotFoundException: jakarta.persistence.Persistencee

This error happens in my standalone Java web-app I am trying to use JPA with it. I have normally used JPA in a container and have done for years. You can view a very simple app here: (https://github.com/pramodShehan5/jpa-example ) that displays the same behavior.

File pom.xml contains the following dependencies

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core-jakarta</artifactId>
        <version>5.6.14.Final</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.6.0</version>
    </dependency>
</dependencies>

When this sample app is compiled with mvn compile all is fine.

cd target/classes
java org.example.Main 

Error

    Exception in thread "main" java.lang.NoClassDefFoundError: jakarta/persistence/Persistence
        at org.example.Main.main(Main.java:21)
    Caused by: java.lang.ClassNotFoundException: jakarta.persistence.Persistence
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)

So the problem as I see it is I need a provider added into my jar and this is where I need help. Can anyone point out what I need to add to my pom file to give me a provider (assuming that is really what I am missing) or do I just need to get one of the GlassFish JARs on the classpath to do this for me?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Damien Cooke
  • 599
  • 10
  • 20
  • You ask a question related to container, but you did not show your `Dockerfile`. You command came to error as expected, because Jakarta EE did not in classpath. – Vy Do May 09 '23 at 23:26
  • If you want to run your project using `java` not `mvn`, then you can collect all the dependent JARs you need (the transitive dependencies downloaded by your POM). See [How extract all the jars needed for a maven project to a folder?](https://stackoverflow.com/q/10294070/12567365), and probably similar questions. Then, make sure these are all on the classpath of your `java` command - something like: `java -cp lib/*.jar java.org.example.Main` – andrewJames May 09 '23 at 23:31
  • Did you include the Jakarta Persistence API as a dependency? – Basil Bourque May 09 '23 at 23:40
  • @BasilBourque I did In the example I mentioned the app was able to satisfy the dependencies from hibernate-core-Jakarta, the issue I think is that I am missing a runtime dependency I am not sure what to resolve it with. – Damien Cooke May 10 '23 at 09:49
  • @andrewJames I believe all the dependencies should be in the jar – Damien Cooke May 10 '23 at 09:55
  • @RaphaëlColantonio Sorry, that was misleading, I was referring to a Java EE Container (Payara or Glassfish for example) – Damien Cooke May 10 '23 at 09:56
  • 1
    What jar do you mean? The command in the question (`java org.example.Main`) does not refer to any jar. Also the jar would need to have been built as a "fat" jar. Was it built that way? – andrewJames May 10 '23 at 10:32

1 Answers1

1

In this particular case it was a matter of putting all the jars that it needs (that would normally be provided by the container) in the modules directory and calling the app like this:

java --class-path "modules/*" --module-path modules com.smartphonedev.mqttlistener.MQTTClient

Damien Cooke
  • 599
  • 10
  • 20