我有一个项目,我在其中解析一些XML文档,而我碰巧需要xerces
依赖项:
<dependency>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
<version>2.4.0</version>
</dependency>
[使用junit4
编写单元测试时,每次运行单元测试时都会遇到问题,以下是每次我使用mvn clean install
进行编译时都会发生的问题:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.346 s <<< FAILURE! - in ConversionTest
[ERROR] ConversionTest.initializationError Time elapsed: 0.054 s <<< ERROR!
java.lang.NoClassDefFoundError: org/w3c/dom/ls/DocumentLS
at ConversionTest.fromDirectory(ConversionTest.java:92)
at ConversionTest.data(ConversionTest.java:65)
Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ls.DocumentLS
at ConversionTest.fromDirectory(ConversionTest.java:92)
at ConversionTest.data(ConversionTest.java:65)
搜索网络,我意识到我需要在pom.xml
中添加新的依赖项:
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
完成此操作后,测试编译正常,我可以生成.jar
,并与以下构建插件打包在一起:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.company.tools.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
...并使用以下设置进行编译:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<encoding>cp1252</encoding>
<release>11</release>
<fork>true</fork>
<meminitial>128m</meminitial>
<maxmem>512m</maxmem>
<compilerArgs>
<arg>-Xpkginfo:always</arg>
</compilerArgs>
</configuration>
</plugin>
这产生了一个.jar
,其中包含所有必需的依赖项,这里包括著名的org/w3c/dom/ls/DocumentLS
:
现在我将此.jar
移动到我的服务器中,并尝试使用以下命令运行它:
java -jar myJar.jar <inputs>
[这样做时,我再次得到以下异常!
Exception in thread "main" java.lang.NoClassDefFoundError: org/w3c/dom/ls/DocumentLS
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:801)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:699)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:622)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:580)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at org.apache.xerces.jaxp.DocumentBuilderImpl.<init>(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.newDocumentBuilder(Unknown Source)
at com.company.tools.impl.FileProviderImpl.getXmlFile(FileProviderImpl.java:68)
at com.company.tools.impl.FileProviderImpl.<init>(FileProviderImpl.java:38)
at com.company.tools.impl.FileProviderImpl$Builder.build(FileProviderImpl.java:91)
at com.company.tools.Application.main(Application.java:50)
Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ls.DocumentLS
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 15 more
我在这里有点迷路。我已经将依赖项添加到了pom.xml
中,该类在.jar
中包装得很好,但是仍然存在相同的问题。我究竟做错了什么?
如果有帮助:
我的机器:
Java version: 11.0.2-BellSoft, vendor: BellSoft, runtime: C:\jdk-11.0.2
Default locale: fr_FR, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
我的服务器:
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
OS: Linux myServerAddress 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
提前感谢!
由于@Sambit对this GitHub issue的第二条评论,我最终找到了解决方案。在这里发布答案,希望可以节省别人的头痛!