java - 可运行的 jar 如何在运行时加载外部 xml 文件?

标签 java xml maven jar

(这似乎是一个足够微不足道的问题,但卡住了 2 天:( )

我有一个可运行的 jar(使用 maven assembly plugin 创建)。 jar 中的类在类路径中查找 xml 文件。但是,我们不想将 xml 文件捆绑在 jar 中,而是希望将其外部化。

尝试到现在:

  1. 在运行时设置类路径:

    java -classpath ./conf -jar my-jar-with-dependencies.jar
    

==> 不加载(conf 文件夹包含 xml)

  1. 在汇编器插件中设置类路径

            <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/

相关文章:

java - 如何迭代不同对象的列表?

android - 具有圆形背景的 TextView

xml - 使用 Qt DOM 更新 XML 文件

java - 如何仅从依赖项 maven 模块加载资源

java - 在另一个 Maven 作业中运行 Maven 作业

java - 如何在 Eclipse 中调试类似于常规 Java 程序的 JUnit 测试

java - Oracle Java 存储库无法编译

java - 在 java 应用程序中实现自定义 jar 时出现错误

maven - maven-bundle-plugin 中的内部错误。嵌入错误 : Opening resource

java.lang.NoSuchMethodError : org. neo4j.ogm.session.SessionFactory.metaData()Lorg/neo4j/ogm/MetaData;