(这似乎是一个足够微不足道的问题,但卡住了 2 天:( )
我有一个可运行的 jar(使用 maven assembly plugin
创建)。 jar 中的类在类路径中查找 xml 文件。但是,我们不想将 xml 文件捆绑在 jar 中,而是希望将其外部化。
尝试到现在:
在运行时设置类路径:
java -classpath ./conf -jar my-jar-with-dependencies.jar
==> 不加载(conf 文件夹包含 xml)
在汇编器插件中设置类路径
<plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <archive> <manifest> <mainClass>com.xxx.Test</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>./conf/</classpathPrefix> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>
==> 不会将ClassPath 添加到可运行jar 中的MANIFEST.MF
编辑:
在jar中生成了MAINFEST.MF
:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: xxx
Build-Jdk: 1.7.0_21
Main-Class: com.xxx.Test
编辑 2:
所以我编辑了 jar 中生成的 MANIFEST
并重新创建了 jar。仍然没有找到 xml!
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: xxx
Build-Jdk: 1.7.0_21
Main-Class: com.xxx.Test
Class-Path: . /* Tried with both . and ./conf */
最佳答案
当您使用 -jar
参数时,您指定的类路径将被忽略。指定为 here
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
JVM 使用 list 中指定的类路径。确保 list 包含类路径定义。
关于java - 可运行的 jar 如何在运行时加载外部 xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30939561/