简体   繁体   中英

Error when running JAR created by Maven Assembly plugin

I am working on Eclipse/Spring/Maven project. My pom.xml looks like this:

 <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.javacodegeeks.snippets.enterprise</groupId> <artifactId>springexample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.2.1</version> <executions> <execution> <id>package-jar-with-dependencies</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.javacodegeeks.snippets.enterprise.App2</mainClass> </manifest> </archive> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version> 3.2.4.RELEASE</version> </dependency> <dependency> <groupId>com.rapplogic</groupId> <artifactId>xbee-api</artifactId> <version> 0.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>jfreechart</groupId> <artifactId>jfreechart</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>jcommon</groupId> <artifactId>jcommon</artifactId> <version>0.9.5</version> </dependency> <dependency> <groupId>org.graphstream</groupId> <artifactId>gs-core</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.graphstream</groupId> <artifactId>gs-ui</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.graphstream</groupId> <artifactId>gs-algo</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.9.Final</version> </dependency> <dependency> <groupId>org.rxtx</groupId> <artifactId>rxtx</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>org.bidib.jbidib.org.qbang.rxtx</groupId> <artifactId>rxtxcomm</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.0.RELEASE</version> </dependency> </dependencies> </project>

My applicationContext.xml looks like this:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 
.
.
.

I do mvn install and then mvn package . I get the output as springexample-0.0.1-SNAPSHOT.jar in the target directory of the project. Then I type the command java - jar springexample-0.0.1-SNAPSHOT.jar and get the following error:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Config
uration problem: Unable to locate Spring NamespaceHandler for XML schema namespa
ce [http://www.springframework.org/schema/tx]
Offending resource: class path resource [applicationContext.xml]

Browsinf through similar problems over Inte.net, I found two possible sources of the error:

  1. Non-specifying the spring library for namespace handling (spring-tx) - done (included in dependencies) 2.Non-specifying applicationContext.xml in the resources of the maven assembly plugin - done (resources tag included in the plugin configuration).

After that, I do not know what could be the problem. Any help appreciated, thanks.

try adding this, instead of 4.0.0.RELEASE:

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

and then do a mvn clean install

I just faced almost the same issue (the app worked using my IDE but not using the generated jar file). I solved it by adding the option <Multi-Release>true</Multi-Release> . This option specifies that your Uber java may contain various Java versions of Java files.

<archive>
  <manifestEntries>
    <Multi-Release>true</Multi-Release>
  </manifestEntries>
  <manifest>
    <mainClass>com.javacodegeeks.snippets.enterprise.App2</mainClass>
  </manifest>
</archive>

See https://maven.apache.org/plugins/maven-compiler-plugin/multirelease.html for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM