加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – runnable jar如何在运行时加载外部xml文件?

发布时间:2020-12-15 00:36:11 所属栏目:Java 来源:网络整理
导读:(这似乎是一个微不足道的问题,但坚持了2天:() 我有一个可运行的jar(使用maven程序集插件创建). jar中的类在类路径中查找xml文件.但是,我们不希望将jar文件捆绑在jar中并希望将其外部化. 试过到现在: 在运行时设置类路径: java -classpath ./conf -jar my-j
(这似乎是一个微不足道的问题,但坚持了2天:()

我有一个可运行的jar(使用maven程序集插件创建). jar中的类在类路径中查找xml文件.但是,我们不希望将jar文件捆绑在jar中并希望将其外部化.

试过到现在:

>在运行时设置类路径:

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

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

>在汇编程序插件中设置classpath

<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添加到runnable 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使用manifest中指定的类路径.确保清单包含类路径定义.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读