问题出现:
编码dubbo服务相关代码之后,因为不想放在容器里面进行运行,所以采用官方推荐的用com.alibaba.dubbo.container.Main加载dubbo服务,但是将dubbo 服务打成jar之后,用java -jar命令进行运行时,一直报错:找不到或无法加载主类 com.alibaba.dubbo.container.Main
问题解决步骤:
1. 通过jd-gui反编译工具查看jar包,打开jar包里面META-INF->MAINFEST.MF文件
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: huangkun
Class-Path: . lib/hua/cloud/cn/common-api/1.0-SNAPSHOT/common-api-1.0-
SNAPSHOT.jar lib/hua/cloud/cn/tax-common/1.0-SNAPSHOT/tax-common-1.0-
SNAPSHOT.jar lib/net/sf/ehcache/ehcache/2.10.0/ehcache-2.10.0.jar lib
/org/mybatis/mybatis-ehcache/1.0.0/mybatis-ehcache-1.0.0.jar lib/io/s
pringfox/springfox-swagger2/2.6.1/springfox-swagger2-2.6.1.jar lib/io
/swagger/swagger-annotations/1.5.10/swagger-annotations-1.5.10.jar li
b/io/swagger/swagger-models/1.5.10/swagger-models-1.5.10.jar lib/io/s
pringfox/springfox-spi/2.6.1/springfox-spi-2.6.1.jar lib/io/springfox
/springfox-core/2.6.1/springfox-core-2.6.1.jar lib/io/springfox/sprin
gfox-schema/2.6.1/springfox-schema-2.6.1.jar lib/io/springfox/springf
ox-swagger-common/2.6.1/springfox-swagger-common-2.6.1.jar lib/io/spr
ingfox/springfox-spring-web/2.6.1/springfox-spring-web-2.6.1.jar lib/
com/google/guava/guava/18.0/guava-18.0.jar lib/com/fasterxml/classmat
e/1.3.1/classmate-1.3.1.jar lib/org/springframework/plugin/spring-plu
gin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar lib/org/s
pringframework/plugin/spring-plugin-metadata/1.2.0.RELEASE/spring-plu
gin-metadata-1.2.0.RELEASE.jar lib/org/mapstruct/mapstruct/1.0.0.Fina
l/mapstruct-1.0.0.Final.jar lib/io/springfox/springfox-swagger-ui/2.6
.1/springfox-swagger-ui-2.6.1.jar lib/hua/cloud/cn/tax-user-api/1.0-S
NAPSHOT/tax-user-api-1.0-SNAPSHOT.jar lib/hua/cloud/cn/tax-user-commo
n/1.0-SNAPSHOT/tax-user-common-1.0-SNAPSHOT.jar lib/hua/cloud/cn/tax-
user-dao/1.0-SNAPSHOT/tax-user-dao-1.0-SNAPSHOT.jar lib/org/springfra
mework/spring-core/4.0.0.RELEASE/spring-core-4.0.0.RELEASE.jar lib/co
mmons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar lib/org
/springframework/spring-web/4.0.0.RELEASE/spring-web-4.0.0.RELEASE.ja
r lib/aopalliance/aopalliance/1.0/aopalliance-1.0.jar lib/org/springf
ramework/spring-beans/4.0.0.RELEASE/spring-beans-4.0.0.RELEASE.jar li
b/org/springframework/spring-context/4.0.0.RELEASE/spring-context-4.0
.0.RELEASE.jar lib/org/springframework/spring-oxm/4.0.0.RELEASE/sprin
g-oxm-4.0.0.RELEASE.jar lib/org/springframework/spring-tx/4.0.0.RELEA
SE/spring-tx-4.0.0.RELEASE.jar lib/org/springframework/spring-jdbc/4.
0.0.RELEASE/spring-jdbc-4.0.0.RELEASE.jar lib/org/springframework/spr
ing-webmvc/4.0.0.RELEASE/spring-webmvc-4.0.0.RELEASE.jar lib/org/spri
ngframework/spring-expression/4.0.0.RELEASE/spring-expression-4.0.0.R
ELEASE.jar lib/org/springframework/spring-aop/4.0.0.RELEASE/spring-ao
p-4.0.0.RELEASE.jar lib/org/springframework/spring-context-support/4.
0.0.RELEASE/spring-context-support-4.0.0.RELEASE.jar lib/org/springfr
amework/spring-test/4.0.0.RELEASE/spring-test-4.0.0.RELEASE.jar lib/o
rg/mybatis/mybatis/3.2.6/mybatis-3.2.6.jar lib/org/mybatis/mybatis-sp
ring/1.2.3/mybatis-spring-1.2.3.jar lib/commons-dbcp/commons-dbcp/1.2
.2/commons-dbcp-1.2.2.jar lib/commons-pool/commons-pool/1.3/commons-p
ool-1.3.jar lib/mysql/mysql-connector-java/5.1.25/mysql-connector-jav
a-5.1.25.jar lib/com/oracle/ojdbc6/11.2.0.1.0/ojdbc6-11.2.0.1.0.jar l
ib/log4j/log4j/1.2.17/log4j-1.2.17.jar lib/org/slf4j/slf4j-log4j12/1.
7.7/slf4j-log4j12-1.7.7.jar lib/org/slf4j/slf4j-api/1.7.7/slf4j-api-1
.7.7.jar lib/commons-fileupload/commons-fileupload/1.3.2/commons-file
upload-1.3.2.jar lib/commons-io/commons-io/2.2/commons-io-2.2.jar lib
/io/jsonwebtoken/jjwt/0.7.0/jjwt-0.7.0.jar lib/com/fasterxml/jackson/
core/jackson-databind/2.2.3/jackson-databind-2.2.3.jar lib/com/faster
xml/jackson/core/jackson-annotations/2.2.3/jackson-annotations-2.2.3.
jar lib/com/fasterxml/jackson/core/jackson-core/2.2.3/jackson-core-2.
2.3.jar lib/com/auth0/java-jwt/3.1.0/java-jwt-3.1.0.jar lib/commons-c
odec/commons-codec/1.10/commons-codec-1.10.jar lib/org/bouncycastle/b
cprov-jdk15on/1.55/bcprov-jdk15on-1.55.jar lib/com/alibaba/fastjson/1
.2.3/fastjson-1.2.3.jar lib/org/apache/commons/commons-lang3/3.3.2/co
mmons-lang3-3.3.2.jar lib/com/alibaba/dubbo/2.5.4/dubbo-2.5.4.jar lib
/org/javassist/javassist/3.20.0-GA/javassist-3.20.0-GA.jar lib/org/jb
oss/netty/netty/3.2.5.Final/netty-3.2.5.Final.jar lib/org/apache/zook
eeper/zookeeper/3.4.8/zookeeper-3.4.8.jar lib/jline/jline/0.9.94/jlin
e-0.9.94.jar lib/io/netty/netty/3.7.0.Final/netty-3.7.0.Final.jar lib
/com/101tec/zkclient/0.10/zkclient-0.10.jar lib/org/apache/shiro/shir
o-spring/1.2.2/shiro-spring-1.2.2.jar lib/org/apache/shiro/shiro-core
/1.2.2/shiro-core-1.2.2.jar lib/commons-beanutils/commons-beanutils/1
.8.3/commons-beanutils-1.8.3.jar lib/org/apache/shiro/shiro-web/1.2.2
/shiro-web-1.2.2.jar lib/commons-collections/commons-collections/3.2.
1/commons-collections-3.2.1.jar lib/org/aspectj/aspectjweaver/1.5.4/a
spectjweaver-1.5.4.jar lib/tk/mybatis/mapper/3.4.0/mapper-3.4.0.jar l
ib/javax/persistence/persistence-api/1.0/persistence-api-1.0.jar lib/
com/github/pagehelper/pagehelper/5.0.0/pagehelper-5.0.0.jar lib/com/g
ithub/jsqlparser/jsqlparser/0.9.5/jsqlparser-0.9.5.jar
Created-By: Apache Maven 3.2.3
Build-Jdk: 1.8.0_131
Main-Class: com.alibaba.dubbo.container.Main
发现Class-path里面的jar包的路径都出现了每个jar包对应的groupId和对应的版本,而我实际package后的target->lib是这样的
通过对比发现是因为jar包里面的class-path路径不对,以jackson-core-2.2.3.jar为例,应该为lib/jackson-core-2.2.3.jar,实际上变成了lib/com/fasterxml/jackson/core/jackson-core/2.2.3/jackson-core-2.2.3.jar,所以确定为应该是maven-jar-plugin插件的问题
2. 出错之前的pom.xml的maven-jar-plugin的配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本-->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
通过查阅官方文档(点击打开链接)发现,是需要自定义MavenRepositoryLayout 。修改如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本-->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}</customClasspathLayout>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
3. 重新进行package之后,再次查看jar包,发现META-INF->MAINFEST.MF文件里面的Class-Path变成了我们想要的格式:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: huangkun
Class-Path: . lib/common-api-1.0-SNAPSHOT.jar lib/tax-common-1.0-SNAPS
HOT.jar lib/ehcache-2.10.0.jar lib/mybatis-ehcache-1.0.0.jar lib/spri
ngfox-swagger2-2.6.1.jar lib/swagger-annotations-1.5.10.jar lib/swagg
er-models-1.5.10.jar lib/springfox-spi-2.6.1.jar lib/springfox-core-2
.6.1.jar lib/springfox-schema-2.6.1.jar lib/springfox-swagger-common-
2.6.1.jar lib/springfox-spring-web-2.6.1.jar lib/guava-18.0.jar lib/c
lassmate-1.3.1.jar lib/spring-plugin-core-1.2.0.RELEASE.jar lib/sprin
g-plugin-metadata-1.2.0.RELEASE.jar lib/mapstruct-1.0.0.Final.jar lib
/springfox-swagger-ui-2.6.1.jar lib/tax-user-api-1.0-SNAPSHOT.jar lib
/tax-user-common-1.0-SNAPSHOT.jar lib/tax-user-dao-1.0-SNAPSHOT.jar l
ib/spring-core-4.0.0.RELEASE.jar lib/commons-logging-1.1.1.jar lib/sp
ring-web-4.0.0.RELEASE.jar lib/aopalliance-1.0.jar lib/spring-beans-4
.0.0.RELEASE.jar lib/spring-context-4.0.0.RELEASE.jar lib/spring-oxm-
4.0.0.RELEASE.jar lib/spring-tx-4.0.0.RELEASE.jar lib/spring-jdbc-4.0
.0.RELEASE.jar lib/spring-webmvc-4.0.0.RELEASE.jar lib/spring-express
ion-4.0.0.RELEASE.jar lib/spring-aop-4.0.0.RELEASE.jar lib/spring-con
text-support-4.0.0.RELEASE.jar lib/spring-test-4.0.0.RELEASE.jar lib/
mybatis-3.2.6.jar lib/mybatis-spring-1.2.3.jar lib/commons-dbcp-1.2.2
.jar lib/commons-pool-1.3.jar lib/mysql-connector-java-5.1.25.jar lib
/ojdbc6-11.2.0.1.0.jar lib/log4j-1.2.17.jar lib/slf4j-log4j12-1.7.7.j
ar lib/slf4j-api-1.7.7.jar lib/commons-fileupload-1.3.2.jar lib/commo
ns-io-2.2.jar lib/jjwt-0.7.0.jar lib/jackson-databind-2.2.3.jar lib/j
ackson-annotations-2.2.3.jar lib/jackson-core-2.2.3.jar lib/java-jwt-
3.1.0.jar lib/commons-codec-1.10.jar lib/bcprov-jdk15on-1.55.jar lib/
fastjson-1.2.3.jar lib/commons-lang3-3.3.2.jar lib/dubbo-2.5.4.jar li
b/javassist-3.20.0-GA.jar lib/netty-3.2.5.Final.jar lib/zookeeper-3.4
.8.jar lib/jline-0.9.94.jar lib/netty-3.7.0.Final.jar lib/zkclient-0.
10.jar lib/shiro-spring-1.2.2.jar lib/shiro-core-1.2.2.jar lib/common
s-beanutils-1.8.3.jar lib/shiro-web-1.2.2.jar lib/commons-collections
-3.2.1.jar lib/aspectjweaver-1.5.4.jar lib/mapper-3.4.0.jar lib/persi
stence-api-1.0.jar lib/pagehelper-5.0.0.jar lib/jsqlparser-0.9.5.jar
Created-By: Apache Maven 3.2.3
Build-Jdk: 1.8.0_131
Main-Class: com.alibaba.dubbo.container.Main