0

So, the software is working properly and everything is fine. I did find some answers to my question online but I can't seem to fix my problem. Everything seems to be okay in my Build Path and in order. The error I'm getting:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
Caused by: java.lang.ClassNotFoundException: org.hibernate.service.ServiceRegistry
        at java.base/java.net.URLClassLoader.findClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        ... 3 more

I need to export my project to a JAR file, please help me, I'm desperate. And thanks to anyone that cares to answer in advance.

J. Gray
  • 1
  • 3
  • How are you running it? Command line? `java -cp path-to-hibernate -jar app.jar`? Double-clicking the jar file? Do you have library paths set in the jar's `MANIFEST.MF`? – AJNeufeld Dec 06 '17 at 18:43
  • Yes I'm running it it cmd `java -jar name.jar` and it gives me that error. I tried exporting in different ways, and it throws even more errors if I export it as runnable. – J. Gray Dec 06 '17 at 18:51
  • I also have a MANIFEST file which reads: `Manifest-Version: 1.0 Main-Class: com.main.Main` – J. Gray Dec 06 '17 at 18:52
  • When I export it as a normal JAR file I get the following error: `Missing JavaFX application class com.main.Main` – J. Gray Dec 06 '17 at 18:54
  • You will need `Class-Path: paths-to-hiberate-etc` in your MANIFEST file. Alternately, you could try to run it as `java -cp 'name.jar:lib/*' com.main.Main` and copy the libraries to a `lib` subfolder beside the jar. – AJNeufeld Dec 06 '17 at 19:06
  • Thank you @AJNeufeld I'll try and do that, though I need a running JAR file (so the people I'm making it for can double-click and start it) – J. Gray Dec 06 '17 at 19:11
  • Possible duplicate of [Call "java -jar MyFile.jar" with additional classpath option](https://stackoverflow.com/questions/15930782/call-java-jar-myfile-jar-with-additional-classpath-option) – AJNeufeld Dec 06 '17 at 19:12
  • Then you'll need to use the `Class-Path: ` in the MANIFEST file, with absolute paths (if it is the same for every user!), or relative paths like `lib/*.jar` and ensure the jar file gets installed along with a `lib` folder, or use a "one-jar" type packaging which packages the main application jar file and all required lib jar files in one super jar. – AJNeufeld Dec 06 '17 at 19:19
  • I'm really having trouble understanding all of this. I'll keep trying. Thanks a ton for your help @AJNeufeld – J. Gray Dec 06 '17 at 19:24

2 Answers2

0

Building a jar-with-dependencies will solve your problem.
Add the following to your pom.xml file:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.main.Main</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

Run mvn clean compile assembly:single
Then you will be able to run java -jar name-with-dependencies.jar

Gal Shaboodi
  • 744
  • 1
  • 7
  • 25
0

So, I've figured out what was the problem. The problem was, I was using Java 8 for my project while I had installed (some time ago) Java 9 on my PC. When I tried to run the JAR file, it used Java 9 to run it and it had some dependencies misplaced. I just uninstalled Java 9, exported the project with jre 1.8.0_144 and I'm running jdk 1.8.0_121 on my PC. Everything works great now.

Thank you @Gal and @AJNeufeld for your answers and your help.

J. Gray
  • 1
  • 3