java.lang.NoClassDefFoundError:由Maven构建后的org / hibernate / service / ServiceRegistry

时间:2018-01-21 13:28:42

标签: java mysql hibernate maven javafx

我正在使用Maven构建应用程序。我在本地MySql中有一个数据库,当它仍然在Eclipse环境中时,一切似乎都没问题。应用程序和数据库之间的连接功能齐全,无需担心。 在Maven,我设定了目标" package"到目前为止没有出现任何问题,但是当应用程序想要进行SELECT ALL查询时,会遇到此错误:

Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
    at festival.AbstractFestivalDAOimpl.open(AbstractFestivalDAOimpl.java:28)
    at festival.FestivalsPane.FestivalDAOImpl.selectAllFestivals(FestivalDAOImpl.java:51)
    at festival.FestivalsPane.FestivalTable.getdata(FestivalTable.java:234)
    at festival.FestivalsPane.FestivalTable.<init>(FestivalTable.java:71)
    at festival.ArtFestivals$1.handle(ArtFestivals.java:148)
    at festival.ArtFestivals$1.handle(ArtFestivals.java:143)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.ClassNotFoundException: org.hibernate.service.ServiceRegistry
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 53 more

这是我的POM.xml

<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>Festival</groupId>
<artifactId>Festival</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
  <dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
  </dependency>
  <dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.4.3</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-tools</artifactId>
    <version>4.3.1.CR1</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.1.Final</version>
</dependency>   
<dependency>
  <groupId>org.javassist</groupId>
  <artifactId>javassist</artifactId>
  <version>3.18.1-GA</version>
</dependency>
<dependency>
  <groupId>org.jboss.logging</groupId>
  <artifactId>jboss-logging</artifactId>
  <version>3.1.3.GA</version>
</dependency>
<dependency>
  <groupId>org.jboss.spec.javax.transaction</groupId>
  <artifactId>jboss-transaction-api_1.2_spec</artifactId>
  <version>1.0.0.Final</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>barcodes</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>font-asian</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>forms</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>hyph</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>io</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>kernel</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>layout</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>pdfa</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>pdftest</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>sign</artifactId>
  <version>7.0.5</version>
</dependency>
<dependency>
  <groupId>com.squareup.picasso</groupId>
  <artifactId>picasso</artifactId>
  <version>2.5.2</version>
</dependency>
<dependency>
  <groupId>org.xerial</groupId>
  <artifactId>sqlite-jdbc</artifactId>
  <version>3.15.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.5</version>
</dependency>


</dependencies>
   <build>
     <sourceDirectory>src</sourceDirectory>
     <resources>
       <resource>
         <directory>src</directory>
         <excludes>
           <exclude>**/*.java</exclude>
         </excludes>
       </resource>
     </resources>
     <plugins>
     <plugin>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>3.5.1</version>
       <configuration>
         <source>1.8</source>
         <target>1.8</target>
       </configuration>
     </plugin>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>festival.ArtFestivals</mainClass>    
                </manifest>
            </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

最后这是我的代码:

public class AbstractFestivalDAOimpl implements FestivalDAO{

    Session session;
    Transaction transaction;

    @Override
    public void open() {
        session = HibernateSessionFact.getSessionFactory().openSession();
        transaction= session.getTransaction();
    }

    @Override
    public void commit(){
         if(!transaction.wasCommitted()){

            transaction = session.beginTransaction();
            transaction.commit();
        }
    }


    @Override
    public void close() {

        session.close();
    }

    @Override
    public void addFestival(CinemaFestival cinemafestival) {
        session.save(cinemafestival);
    }

    @Override
    public void deleteFestival(CinemaFestival cinemafestival) {
        session.delete(cinemafestival);
    }

    @Override
    public void updateFestival(CinemaFestival cinemafestival) {
        session.update(cinemafestival);
    }

    @Override
    public ObservableList<CinemaFestival> selectAllFestivals(){
        String selall = "from festival.CinemaFestival";
        Query query = session.createQuery(selall);
        ObservableList<CinemaFestival> festlist = FXCollections.observableArrayList();
        List<CinemaFestival> list = query.list();
        for(Object o : list){
            CinemaFestival c = (CinemaFestival) o;
            festlist.add(c);
        }
        close();
        return festlist;
    }

    @Override
    public ObservableList<CinemaFestival> instantSearchFestivals(String key){
        String[] stringdata = {"fullname", "abbr" , "Country" , "City" ,
            "Website"};
        ObservableList<CinemaFestival> festlist = FXCollections.observableArrayList();

        for (String s: stringdata){
            String selwhe = "from festival.CinemaFestival where " + s + " like :key";

            Query query = session.createQuery(selwhe);
            query.setParameter("key", "%" +key + "%");

            List<CinemaFestival> list = query.list();
            for(Object o : list){
            CinemaFestival c = (CinemaFestival) o;
            if (!festlist.contains(c)){
            festlist.add(c);
            }
        }
        }
        return festlist;
    }

    @Override
    public ObservableList<CinemaFestival> advancedSearchFestivals(String s , HashMap <String,Object> map){

        ObservableList<CinemaFestival> resultlist = FXCollections.observableArrayList();

        Query query = session.createQuery(s);
        for (String key: map.keySet()){
            if (map.get(key) instanceof String){
            query.setParameter(key, "%" + map.get(key) + "%");
            }
            if (map.get(key) instanceof Boolean){
                query.setParameter(key, map.get(key));
            }
            if (map.get(key) instanceof Integer){
                query.setParameter(key, map.get(key));
            }
        }
            List list = query.list();
            for(Object o : list){
            CinemaFestival c = (CinemaFestival) o;
            if (!resultlist.contains(c)){
            resultlist.add(c);
            }
        }

        return resultlist;
    }

    @Override
    public void addClient(Client client) {
        session.save(client);
    }

    @Override
    public void deleteClient(Client client) {
        session.delete(client);
    }

    @Override
    public void updateClient(Client client) {
        session.update(client);
    }

    @Override
    public ObservableList<Client> selectAllClients(){
        String selall = "from festival.Client";
        Query query = session.createQuery(selall);
        ObservableList<Client> clientlist = FXCollections.observableArrayList();
        List<Client> list = query.list();
        for(Object o : list){
            Client c = (Client) o;
            clientlist.add(c);
        }
        close();
        return clientlist;
    }

    @Override
    public ObservableList<Client> instantSearchClients(String key){
        String[] stringdata = {"firstname", "surname" , "job1" , "job2" ,
            "mobile1" , "mobile2" , "home1" , "home2" , "work1" , "work2" , "other1" , "other2" };
        ObservableList<Client> clientlist = FXCollections.observableArrayList();

        for (String s: stringdata){
            String selwhe = "from festival.Client where " + s + " like :key";

            Query query = session.createQuery(selwhe);
            query.setParameter("key", "%" +key + "%");

            List<Client> list = query.list();
            for(Object o : list){
            Client c = (Client) o;
            if (!clientlist.contains(c)){
            clientlist.add(c);
            }
        }
        }
        return clientlist;
    }

    @Override
    public void addMovie(Movie movie) {
        session.save(movie);
    }

    @Override
    public void deleteMovie(Movie movie) {
        session.delete(movie);
    }

    @Override
    public void updateMovie(Movie movie) {
        session.update(movie);
    }

    @Override
    public ObservableList<Movie> selectAllMovies(){
        String selall = "from festival.Movie";
        Query query = session.createQuery(selall);
        ObservableList<Movie> movielist = FXCollections.observableArrayList();
        List<Movie> list = query.list();
        for(Object o : list){
            Movie c = (Movie) o;
            movielist.add(c);
        }
        close();
        return movielist;
    }

    @Override
    public ObservableList<Movie> instantSearchMovies(String key){
        String[] stringdata = {"fullname", "language" , "section" , "genre"};
        ObservableList<Movie> movielist = FXCollections.observableArrayList();

        for (String s: stringdata){
            String selwhe = "from festival.Movie where " + s + " like :key";

            Query query = session.createQuery(selwhe);
            query.setParameter("key", "%" +key + "%");

            List<Movie> list = query.list();
            for(Object o : list){
            Movie c = (Movie) o;
            if (!movielist.contains(c)){
            movielist.add(c);
            }
        }
        }
        return movielist;
    }

    @Override
    public void addSubmit(Submitionrecord submit) {
        session.save(submit);
    }

    @Override
    public void deleteSubmit(Submitionrecord submit) {
        session.delete(submit);
    }

    @Override
    public void updateSubmit(Submitionrecord submit) {
        session.update(submit);
    }

    @Override
    public ObservableList<Submitionrecord> selectAllSubmits(){
        String selall = "from festival.Submitionrecord";
        Query query = session.createQuery(selall);
        ObservableList<Submitionrecord> submitlist = FXCollections.observableArrayList();
        List<Submitionrecord> list = query.list();
        for(Object o : list){
            Submitionrecord s = (Submitionrecord) o;
            submitlist.add(s);
        }
        close();
        return submitlist;
    }

    @Override
    public ObservableList<Submitionrecord> instantSearchSubmits(String key){
        String[] stringdata = {"fullname", "language" , "section" , "genre" , "duration"};
        ObservableList<Submitionrecord> submitlist = FXCollections.observableArrayList();

        for (String s: stringdata){
            String selwhe = "from festival.Submitionrecord where " + s + " like :key";

            Query query = session.createQuery(selwhe);
            query.setParameter("key", "%" +key + "%");

            List<Submitionrecord> list = query.list();
            for(Object o : list){
                Submitionrecord s1 = (Submitionrecord) o;
            if (!submitlist.contains(s1)){
            submitlist.add(s1);
            }
        }
        }
        return submitlist;
    }
}

第28行是:

session = HibernateSessionFact.getSessionFactory().openSession();

这是mvn依赖:树结果

[INFO] +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] +- net.sf.ehcache:ehcache-core:jar:2.4.3:compile
[INFO] +- org.hibernate:hibernate-tools:jar:4.3.1.CR1:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.4.Final:compile
[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.1.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  +- javax.transaction:jta:jar:1.1:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.1:compile
[INFO] |  |  +- logkit:logkit:jar:1.0.1:compile
[INFO] |  |  +- avalon-framework:avalon-framework:jar:4.1.3:compile
[INFO] |  |  \- javax.servlet:servlet-api:jar:2.3:compile
[INFO] |  +- ant:ant:jar:1.6.5:compile
[INFO] |  +- freemarker:freemarker:jar:2.3.8:compile
[INFO] |  +- org.hibernate:jtidy:jar:r8-20060801:compile
[INFO] |  +- org.eclipse.tycho:org.eclipse.jdt.core:jar:3.9.1.v20130905-0837:compile
[INFO] |  +- org.eclipse:text:jar:3.3.0-v20070606-0010:compile
[INFO] |  |  \- org.eclipse.core:commands:jar:3.3.0-I20070605-0010:compile (version selected from constraint [3.2.0,4.0.0))
[INFO] |  +- org.eclipse.core:runtime:jar:3.9.0-v20130326-1255:runtime
[INFO] |  |  +- org.eclipse:osgi:jar:3.10.0-v20140606-1445:runtime (version selected from constraint [3.7.0,4.0.0))
[INFO] |  |  +- org.eclipse.core:jobs:jar:3.6.0-v20140424-0053:runtime (version selected from constraint [3.2.0,4.0.0))
[INFO] |  |  +- org.eclipse.equinox:registry:jar:3.5.400-v20140428-1507:runtime (version selected from constraint [3.4.0,4.0.0))
[INFO] |  |  +- org.eclipse.equinox:preferences:jar:3.5.200-v20140224-1527:runtime (version selected from constraint [3.4.0,4.0.0))
[INFO] |  |  +- org.eclipse.core:contenttype:jar:3.4.200-v20140207-1251:runtime (version selected from constraint [3.3.0,4.0.0))
[INFO] |  |  \- org.eclipse.equinox:app:jar:1.3.200-v20130910-1609:runtime (version selected from constraint [1.0.0,2.0.0))
[INFO] |  +- org.eclipse.equinox:common:jar:3.6.200-v20130402-1505:compile
[INFO] |  \- antlr:antlr:jar:2.7.7:compile
[INFO] +- org.hibernate:hibernate-core:jar:4.3.1.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] +- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.6.1:compile
[INFO] |  \- log4j:log4j:jar:1.2.16:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.6.1:compile
[INFO] +- com.itextpdf:barcodes:jar:7.0.5:compile
[INFO] +- com.itextpdf:font-asian:jar:7.0.5:compile
[INFO] +- com.itextpdf:forms:jar:7.0.5:compile
[INFO] +- com.itextpdf:hyph:jar:7.0.5:compile
[INFO] +- com.itextpdf:io:jar:7.0.5:compile
[INFO] +- com.itextpdf:kernel:jar:7.0.5:compile
[INFO] +- com.itextpdf:layout:jar:7.0.5:compile
[INFO] +- com.itextpdf:pdfa:jar:7.0.5:compile
[INFO] +- com.itextpdf:pdftest:jar:7.0.5:compile
[INFO] |  +- ch.qos.logback:logback-classic:jar:1.1.3:compile
[INFO] |  |  \- ch.qos.logback:logback-core:jar:1.1.3:compile
[INFO] |  \- junit:junit:jar:4.12:compile
[INFO] |     \- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] +- com.itextpdf:sign:jar:7.0.5:compile
[INFO] +- com.squareup.picasso:picasso:jar:2.5.2:compile
[INFO] +- org.xerial:sqlite-jdbc:jar:3.15.1:compile
[INFO] \- mysql:mysql-connector-java:jar:5.1.5:compile

任何人都知道为什么我会收到这个例外?

2 个答案:

答案 0 :(得分:0)

这不是一个确切的答案。但请尝试以下步骤找出问题。

  1. 使用类名 ServiceRegistry 在日食中执行Ctrl + Alt + T.如果您获得该类,则将其跟踪到它所在的jar文件。还要记下版本。
  2. 使用此命令获取您的依赖关系树 mvn依赖:树
  3. 检查依赖关系树结果中是否有步骤1中具有确切版本的jar。
  4. 正如您所说,您使用作为maven目标。您可能在部署目标的类路径中缺少依赖的hibernate-core jar。 例如,对于Web应用程序。 jar应该位于服务器中webapps文件夹的 WEB-INF / lib 文件夹中。

答案 1 :(得分:0)

我终于设法回答了我的问题。这个问题实际上很明显,正是因为缺乏经验。 在POM文件中,我需要向清单添加一个类路径,我这样做:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>main.java.festival.ArtFestivals</mainClass>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
  </plugin>

并且有效。