0

When running java hwai.Main from directory target/classes, I get an error:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Persistence
at hwai.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
...

I get the same error when I'm running java hwai.Main from directory target/test1/WEB-INF/classes This is my project structure after running mvn package glassfish:redeploy:

├── pom.xml
├── src
│   └── main
│       ├── java
│       │   ├── hwai
│       │   │   ├── helloWorld.java
│       │   │   ├── helloWorldResource.java
│       │   │   ├── Main.java
│       │   │   ├── StudentPOJO.java
│       │   │   ├── StudentRest.java
│       │   │   └── Todo.java
│       │   └── META-INF
│       │       └── persistence.xml
│       └── webapp
│           ├── index.html
│           └── WEB-INF
│               └── web.xml
└── target
    ├── classes
    │   └── hwai
    │       ├── helloWorld.class
    │       ├── helloWorldResource.class
    │       ├── Main.class
    │       ├── StudentPOJO.class
    │       ├── StudentRest.class
    │       └── Todo.class
    ├── generated-sources
    │   └── annotations
    ├── maven-archiver
    │   └── pom.properties
    ├── maven-status
    │   └── maven-compiler-plugin
    │       └── compile
    │           └── default-compile
    │               ├── createdFiles.lst
    │               └── inputFiles.lst
    ├── test1
    │   ├── index.html
    │   ├── META-INF
    │   └── WEB-INF
    │       ├── classes
    │       │   └── hwai
    │       │       ├── helloWorld.class
    │       │       ├── helloWorldResource.class
    │       │       ├── Main.class
    │       │       ├── StudentPOJO.class
    │       │       ├── StudentRest.class
    │       │       └── Todo.class
    │       ├── lib
    │       │   ├── commonj.sdo-2.1.1.jar
    │       │   ├── eclipselink-2.7.0.jar
    │       │   ├── javax.json-1.0.4.jar
    │       │   ├── javax.persistence-2.0.0.jar
    │       │   └── validation-api-1.1.0.Final.jar
    │       └── web.xml
    └── test1.war

Searching the internet, I found Eclipse Warnings: class javax.persistence.* not found and java.lang.NoClassDefFoundError: javax/persistence/Persistence, but neither helped me. I'm quite a javaee newbie.

pom.xml contains among others

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
    <scope>compile</scope>
</dependency>

The import - statement in Main.java is

import javax.persistence.Persistence;
Markus
  • 578
  • 6
  • 26
  • 2
    You need to learn about the classpath. Any library you're using must be in the classpath, otherwise Java can't possibly find the classes your code is using. This is Java fundamental stuff. Learn the basics before doing complex stuff like Maven and JPA. https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html – JB Nizet Jul 08 '18 at 17:34
  • Thanks! Calling `java -cp "./:../lib/*" hwai.Main` from dir `target/test1/WEB-INF/classes` did the trick... Now I get `No Persistence provider for EntityManager named ...` which might result from misplaced persistence.xml (though I put it in the classpath), but that's another story... – Markus Jul 08 '18 at 18:48
  • `mkdir -p target/test1/WEB-INF/classes/META-INF` and `cp persistence.xml target/test1/WEB-INF/classes/META-INF` helped, after reading https://stackoverflow.com/questions/3230798/eclipselink-no-persistence-provider-for-entitymanager-named – Markus Jul 08 '18 at 19:04

0 Answers0