1

I have a strange problem with my Spring application. I use Eclipse and when i click RUn as Java Application my App works like a charm, but when i click maven build (mvn install) after packing to jar and try to run i get:

   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

I gues that the problem is in this line: 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();

I have all jar in maven dependencies, but still i cant solve this wired problem. Please help me.

UPDATE: My 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>

UPDATE Uberjar works like a charm Now i have another problem, after run jar file i get:

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) 

And also i cant add or change entities. After make customerDao.save(Customer) i get only in hibernate

Hibernate: select max(id) from customers

But no record in DB shows up.

  • Please check that you *really* have all Spring dependencies in you `pom.xml`. –  Sep 05 '13 at 08:07
  • this error comes when any of the classes are present at compile time but not at runtime so please make sure that you have all the required classes while you run your application – ankit Sep 05 '13 at 08:32
  • I have added my pom.xml. – user2746686 Sep 05 '13 at 08:39

2 Answers2

1

I believe that you are running jar file from command line using java -jar In this case you will need to specify all jars which needs to be included in classpath using -classpath option.

Another option would be using Maven uber jar (jar which contains all depencies.)

Vinay Lodha
  • 2,185
  • 20
  • 29
0

The various spring jars each contain their own spring.handlers and spring.schemas files named identically, meaning the shader/uberjar plugins will pick one of them and other namespaces will fail to work.

See Invalid or corrupt JAR File built by Maven shade plugin for a shader plugin configuration merging the otherwise conflicting files. That example does have the configuration element in a weird location, my version is:

        <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>
Community
  • 1
  • 1
Stefan L
  • 1,529
  • 13
  • 20