1

我的 Spring 应用程序有一个奇怪的问题。我使用 Eclipse,当我单击作为 Java 应用程序运行时,我的应用程序就像一个魅力,但是当我在打包到 jar 并尝试运行后单击 maven build (mvn install) 时,我得到:

   Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.Applica
tionContext
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        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)
        ... 6 more

我猜问题出在这一行: public static void main(String[] args) {

ModernSoft ms = new ModernSoft();
StringBuffer classpath = new StringBuffer();
 ClassLoader applicationClassLoader = ms.getClass().getClassLoader();
 if (applicationClassLoader == null) {
     applicationClassLoader = ClassLoader.getSystemClassLoader();
 }
 URL[] urls = ((URLClassLoader)applicationClassLoader).getURLs();
  for(int i=0; i < urls.length; i++) {
      classpath.append(urls[i].getFile()).append("\r\n");
  }    
  System.out.println(classpath.toString());
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:**/applicationContext*.xml");
//org.springframework.context.ApplicationContext ctx = new FileSystemXmlApplicationContext("C:\\Documents and Settings\\BK\\workspace\\ModernSoft\\src\\main\\resources\\applicationContext.xml");
DataSource ds = (DataSource) ctx.getBean("dataSource", DataSource.class);
String[] beans = ctx.getBeanDefinitionNames();

我在 Maven 依赖项中有所有 jar,但我仍然无法解决这个有线问题。请帮我。

更新:我的 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>ModernSoft</groupId>
    <artifactId>pl.modern</artifactId>
    <version>0.0.1</version>
    <name>ModernSoft</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                    <descriptorRefs>  
                        <descriptorRef>jar-with-dependencies</descriptorRef>  
                    </descriptorRefs>  
                </configuration>
            </plugin>
        </plugins>
        <finalName>ModernSoft</finalName>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.3.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.0.Final</version>
        </dependency>



    </dependencies>
</project>

更新 Uberjar 就像一个魅力现在我有另一个问题,运行 jar 文件后我得到:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to lo
cate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa]                                  
Offending resource: class path resource [applicationContext.xml]                                                                        

        at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)                     
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)                                         
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)                                         
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:318)              
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1435)

        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)

        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentR
eader.java:185)                                                                                                                         
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocu
mentReader.java:139)                                                                                                                    
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocume
ntReader.java:108)                                                                                                                      
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)      
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)        
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)          
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)          
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
174)                                                                                                                                    
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
209)                                                                                                                                    
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
180)                                                                                                                                    
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
243)                                                                                                                                    
        at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)

        at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93) 
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationCo
ntext.java:130)                                                                                                                         
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)   
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)                  
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)           
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)            
        at pl.modern.ModernSoft.main(ModernSoft.java:27) 

而且我也无法添加或更改实体。在 make customerDao.save(Customer) 之后,我只能进入休眠状态

Hibernate: select max(id) from customers

但是数据库中没有记录出现。

4

2 回答 2

1

我相信您正在从命令行运行 jar 文件,java -jar在这种情况下,您需要使用选项指定所有需要包含在类路径中的 jar -classpath

另一种选择是使用Maven uber jar(包含所有依赖项的 jar。)

于 2013-09-05T08:44:50.717 回答
0

各种 spring jar 都包含它们自己的 spring.handlers 和 spring.schemas 文件,名称相同,这意味着着色器/uberjar 插件将选择其中一个,而其他命名空间将无法工作。

有关合并其他冲突文件的着色器插件配置,请参阅Maven 阴影插件构建的无效或损坏的 JAR文件。该示例确实在一个奇怪的位置有配置元素,我的版本是:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>

                </execution>
            </executions>
            <configuration>
                <transformers>
                    <!-- must be SURE to do this with both spring.handlers and spring.schemas.
                        otherwise you won't be able to use them in the spring config files. -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>

                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                </transformers>
            </configuration>
        </plugin>
于 2014-07-18T08:55:42.140 回答