maven项目生成的jar包运行java -jar 包名报错 Configuration problem: Unable to locate Spring NamespaceHandler for X

Posted 风中的四月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven项目生成的jar包运行java -jar 包名报错 Configuration problem: Unable to locate Spring NamespaceHandler for X相关的知识,希望对你有一定的参考价值。

1.我的maven项目在eclipse中Run As->Maven install,生成jar包后,用java -jar 包名 运行报错。报错图如下:

2.百度查了很多,有说版本号不一致的,有说缺少jar包的,有说用maven-shade-plugin插件的。

3.下面粘贴一下我的pom.xml和applicationContext.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>com.oscar.bdc.rpc</groupId>
  <artifactId>rpcthriftserver</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>rpcthriftserver</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.1.9.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.11.0</version>
      </dependency>
       <dependency>
	        <groupId>commons-io</groupId>
	        <artifactId>commons-io</artifactId>
	        <version>2.6</version>
       </dependency>
       <!-- SPRING begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
			
		<!-- spring orm -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<!-- SPRING end -->
  </dependencies>
  	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
		      <artifactId>maven-assembly-plugin</artifactId>
		      <configuration>
		        <archive>
		          <manifest>
		         	 <addClasspath>true</addClasspath>
					 <classpathPrefix>lib/</classpathPrefix>
  		             <mainClass>com.oscar.bdc.rpc.RPCThriftServerApplication</mainClass>
		          </manifest>
		        </archive>
		        <descriptorRefs>
		          <descriptorRef>jar-with-dependencies</descriptorRef>
		        </descriptorRefs>
		      </configuration>
		      <executions>  
			    <execution>  
			      <id>make-assembly</id>  
			      <phase>package</phase>  
			      <goals>  
			        <goal>single</goal>  
			      </goals>  
			   </execution>  
			  </executions>  
		      
		    </plugin>
        </plugins>
    </build>
</project>

   

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"
	default-lazy-init="true">

	<description>Spring Configuration</description>
	
	
	<context:component-scan base-package="com.oscar.bdc.rpc">	</context:component-scan>
      	
	
</beans>

 4.先把pom.xml缺少的jar包在applicationContext.xml中删掉引用,这个方案不可以。

   5. 然后maven-shade-plugin这个插件,之前加了这个插件到pom.xml中不可以。

   6.特别注意:是把maven-shade-plugin插件替换了maven-assembly-plugin插件。注意是替换。替换了就可以用了。运行没问题。下面粘贴替换后的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>com.oscar.bdc.rpc</groupId>
  <artifactId>rpcthriftserver</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>rpcthriftserver</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.1.9.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.11.0</version>
      </dependency>
       <dependency>
	        <groupId>commons-io</groupId>
	        <artifactId>commons-io</artifactId>
	        <version>2.6</version>
       </dependency>
       <!-- SPRING begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
			
		<!-- spring orm -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<!-- SPRING end -->
  </dependencies>
  	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <!-- <plugin>
		      <artifactId>maven-assembly-plugin</artifactId>
		      <configuration>
		        <archive>
		          <manifest>
		         	 <addClasspath>true</addClasspath>
					 <classpathPrefix>lib/</classpathPrefix>
  		             <mainClass>com.oscar.bdc.rpc.RPCThriftServerApplication</mainClass>
		          </manifest>
		        </archive>
		        <descriptorRefs>
		          <descriptorRef>jar-with-dependencies</descriptorRef>
		        </descriptorRefs>
		      </configuration>
		      <executions>  
			    <execution>  
			      <id>make-assembly</id>  
			      <phase>package</phase>  
			      <goals>  
			        <goal>single</goal>  
			      </goals>  
			   </execution>  
			  </executions>  
		      
		    </plugin> -->
		    
			<plugin> 
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>3.1.1</version> 
				<executions> 
					<execution> 
						<phase>package</phase> 
							<goals>
								<goal>shade</goal> 
							</goals> 
							<configuration>
								 <transformers> 
								 	<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> 
								 	<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.oscar.bdc.rpc.RPCThriftServerApplication</mainClass> </transformer> 
								 	</transformers>
							 </configuration> 
						</execution>
				 </executions>
			</plugin> 
        </plugins>
    </build>
</project>

  

以上是关于maven项目生成的jar包运行java -jar 包名报错 Configuration problem: Unable to locate Spring NamespaceHandler for X的主要内容,如果未能解决你的问题,请参考以下文章

maven项目生成war包

Eclipse创建Maven项目

Maven--Maven 安装最佳实践

maven下的javadoc在哪

Maven 专题:常用命令

Maven和Nexus