加载相对于执行jar文件的文件 [英] Loading a file relative to the executing jar file

查看:121
本文介绍了加载相对于执行jar文件的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明了一切。

我的特殊情况是当前工作目录不是jar文件的位置,但是 c:\ Windows\system32 (我的jar文件由windows使用右键菜单启动,我想将文件夹的路径作为参数传递给jar)。

The specialty in my case is that the current working directory is not the location of the jar file but c:\Windows\system32 (My jar file is started by windows using the right-click-menu, i want to pass a folder's path as a parameter to the jar).

现在我想加载一个名为 config.xml 的配置文件,它与jar在同一个文件夹中。当然,该文件的目的是为jar提供设置。对我来说很重要的是,xml文件是jar文件的外部,以便于编辑。

Now I want to load a configuration file called config.xml that is in the same folder as the jar. The purpose of the file is, of course, to provide settings for the jar. It is important for me that the xml file is outside of the jar file for easy editing.

我很难加载文件。 Windows执行行

I'm having a hard time loading that file. Windows executes the line

cmd /k java -jar D:\pathToJarfile\unpacker-0.0.1-SNAPSHOT-jar-with-dependencies.jar

cmd / k <调用整个事物打开windows命令提示符以便我可以看到jar的输出。

Calling the whole thing with cmd /k leaves the windows command prompt open so that I can see the jar's output.

我不能使用新文件(。 ) System.getProperty(user.dir)用于相对路径,因为这些函数返回 C:\\ \\ windows\system32 \。 C:\ Windows \ system32 ,分别是Windows执行的所有内容的工作文件夹AFAIK)。

I cannot use new File(".") or System.getProperty("user.dir") for the relative path as these functions return C:\Windows\system32\. and C:\Windows\system32, respectively (which is the working folder for everything that windows executes AFAIK).

我没有成功使用 Launcher.class.getResourceAsStream(/../ config.xml)要么。由于该路径以 / 开头,因此搜索从jar的根节点开始。转到 ../ config.xml 指向该文件,但调用返回 null

I've had no success with Launcher.class.getResourceAsStream("/../config.xml") either. Since that path begins with /, searching starts at the root node of the jar. Going to ../config.xml point exactly to that file, but the call returns null.

有人能指出我正确的方向吗?我真的被困在这里了。这个文件加载的东西真的每次都让我感到烦恼...

Can someone point me in the right direction? I'm really stuck here. This file loading stuff really bugs me everytime...

我自己的要求:


  • 我不想在java源代码中硬编码路径

  • 我不想将文件的路径作为参数传递给 java - jar 调用(既不作为 main(String [] args)的参数,也不使用 -Dpath = d:\\ \\ ... 设置系统属性)

  • I don't want to hardcode the path in the java source code
  • I don't want to pass the file's path as a parameter to the java -jar call (neither as a param to the main(String[] args) nor using -Dpath=d:\... to set a system property)

除了原来的问题,我还很难将maven2放在 Class-Path:。进入 MANIFEST.MF jar-with-dependencies 时$ c>(BalusC发布的解决方案)。
问题是该行出现在常规jar的MANIFEST文件中,但不是jar-with-dependencies.jar的MANIFEST文件(生成了2个jar文件)。
对于任何关心我如何做的人:

In addition to the original problem, I had a hard time to have maven2 place Class-Path: . into the MANIFEST.MF (The solution that BalusC posted) when using jar-with-dependencies. The problem was that the line appeared in the regular jar's MANIFEST file, but not the jar-with-dependencies.jar's MANIFEST file (2 jar files are generated). For anyone who cares how i did it:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-5</version>
    <configuration>
      <archive>
        <manifest>
          <mainClass>${mainClass}</mainClass>
          <addClasspath>true</addClasspath>
          <!--at first, i tried to place the Class-Path entry
              right here using <manifestEntries>. see below -->
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>attached</goal>
        </goals>
        <phase>package</phase>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>${mainClass}</mainClass>
            </manifest>
            <!--this is the correct placement -->
            <manifestEntries>
              <Class-Path>.</Class-Path>
            </manifestEntries>
          </archive>
        </configuration>
      </execution>
    </executions>
  </plugin>


推荐答案

获取 Launcher.class .getResourceAsStream(/../ config.xml)要工作,你需要将其路径添加到 Class-Path 条目中JAR的 MANIFEST.MF 文件。这是正常做法。

To get Launcher.class.getResourceAsStream("/../config.xml") to work, you need to add its path to the Class-Path entry of the JAR's MANIFEST.MF file. This is the normal practice.

这篇关于加载相对于执行jar文件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆