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

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

问题描述

问题说明了一切.

我的情况的特点是当前工作目录不是 jar 文件的位置,而是 c:Windowssystem32(我的 jar 文件由 windows 使用右键单击启动 -菜单,我想将文件夹的路径作为参数传递给 jar).

The specialty in my case is that the current working directory is not the location of the jar file but c:Windowssystem32 (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:pathToJarfileunpacker-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.

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

I cannot use new File(".") or System.getProperty("user.dir") for the relative path as these functions return C:Windowssystem32. and C:Windowssystem32, 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(BalusC贴出来的解决方案)使用 jar-with-dependencies 时.问题是该行出现在常规 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") 工作,你需要添加它的JAR 的 MANIFEST.MF 文件的 Class-Path 条目的路径.这是正常做法.

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天全站免登陆