0

I have a project with Maven, JPA and JavaFX. I execute install in maven. Everything is going fine. But when I run the jar file I get stacktrace with exceptions.

pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.pushkartech</groupId>
  <artifactId>Schedule</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>com.zenjava</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <mainClass>com.devcolibri.mavenjavafxapp.MainApp</mainClass>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
          <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>true</addClasspath>
                  <classpathPrefix>lib/</classpathPrefix>
                  <mainClass>Main.MainWindow</mainClass>
                </manifest>
              </archive>
            </configuration>
          </plugin>
        </plugins>
      </build>

  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.3.7.Final</version>
    </dependency>

    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>javax.persistence-api</artifactId>
      <version>2.2</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.2.Final</version>
    </dependency>

    <dependency>
      <groupId>org.apache.geronimo.specs</groupId>
      <artifactId>geronimo-osgi-locator</artifactId>
      <version>1.1</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.12</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>4.3.6.Final</version>
    </dependency>

    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>


  </dependencies>

</project>

Also my stacktrace

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at Main.MainWindow.start(MainWindow.java:24)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.Trampoline.invoke(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
        ... 17 more
Caused by: java.lang.NoClassDefFoundError: javax/persistence/Persistence
        at DAO.aDAOManager.<clinit>(aDAOManager.java:9)
        at Model.Managers.WorkerManager.<init>(WorkerManager.java:12)
        at Controller.MainController.initialize(MainController.java:46)
        ... 27 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

I google this question but I didn't find something that would help me.

If I commented two rows in code where I referring to DB then application starts. And all right. My app can't find the class of persistence. What did I do wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Anton
  • 41
  • 9
  • `java.lang.ClassNotFoundException: javax.persistence.Persistence`. So that class is not in the classpath at RUNTIME. So put the requisite jar (javax.persistence jpa-api) in the classpath. Basic java. Also you currently seem to be wanting to put JPA 2.2 API jar and JPA 2.1 API jar in the classpath ... make your mind up what you are using ... –  Nov 04 '18 at 07:18

2 Answers2

0

If you use Java 11 you simply do not have required modules as they were removed. Please see e.g.: https://winterbe.com/posts/2018/08/29/migrate-maven-projects-to-java-11-jigsaw/ or https://blog.codefx.org/java/java-11-migration-guide/#Migrating-From-Java-8-To-Java-11

piradian
  • 414
  • 7
  • 19
  • Thanks for answer. I use Java 8 – Anton Nov 03 '18 at 19:28
  • :). Then maybe you have wrong version of JPA API implementation? You intend to use JPA 2.2 but your Hibernate dependency might not support it: https://stackoverflow.com/questions/46951817/from-which-version-on-will-hibernate-support-jpa-2-2 – piradian Nov 03 '18 at 19:37
  • Then I have the question. Why my app starts in IDEA with hibernate 5.3.7 and JPA 2.2 but jar? doesn't start? – Anton Nov 03 '18 at 20:00
  • also, i had tried to use JPA 2.1. And i didn't get the result:( – Anton Nov 03 '18 at 20:05
  • also Hibernate 5.3.7 is compatible with jpa 2.2 – Anton Nov 03 '18 at 20:12
  • Thank you. I will try to do it – Anton Nov 03 '18 at 20:22
  • Just to be sure :) Are you check if you use Java 8 at runtime? You may have Java 8 at compile time but when run it, it is possible that you have other JRE configured as runtime (and it might be >8). At least I had once such a problem ;) – piradian Nov 04 '18 at 06:44
0

Problem was in build with maven. A added plugin for build with dependecies and it all worked. That plugin.

<plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>your main class</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
Anton
  • 41
  • 9