加载相对于执行jar文件的文件

f1s*_*1sh 13 java resources jar path

问题就是这一切.

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

现在我想加载一个名为config.xmljar 的配置文件.当然,该文件的目的是为jar提供设置.这对我来说重要的是,XML文件,以方便编辑的jar文件.

我很难加载该文件.Windows执行该行

cmd /k java -jar D:\pathToJarfile\unpacker-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Run Code Online (Sandbox Code Playgroud)

调用整个东西cmd /k会使windows命令提示符处于打开状态,以便我可以看到jar的输出.

我不能使用new File(".")System.getProperty("user.dir")为相对路径,因为这些函数返回C:\Windows\system32\.C:\Windows\system32分别,(这对于窗口执行AFAIK一切工作文件夹).

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

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

我自己的要求:

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

Sea*_*oyd 12

以下是使用Class.getProtectionDomain()的一种可能的解决方案:

final Class<?> referenceClass = YourMainClass.class;
final URL url =
    referenceClass.getProtectionDomain().getCodeSource().getLocation();

try{
    final File jarPath = new File(url.toURI()).getParentFile();
    System.out.println(jarPath); // this is the path you want 
} catch(final URISyntaxException e){
    // etc.
}
Run Code Online (Sandbox Code Playgroud)

YourMainClass 可以用jar中的任何类替换.


Class.getProtectionDomain()文档:

Returns the ProtectionDomain of this class.
If there is a security manager installed, this method first calls
the security manager's checkPermission method with a
RuntimePermission("getProtectionDomain") permission to ensure it's
ok to get the ProtectionDomain.

Returns:
  the ProtectionDomain of this class
Throws:
  SecurityException - if a security manager exists and its
  checkPermission method doesn't allow getting the ProtectionDomain.
Run Code Online (Sandbox Code Playgroud)


Bal*_*usC 6

要开始Launcher.class.getResourceAsStream("/../config.xml")工作,您需要将其路径添加到Class-PathJAR MANIFEST.MF文件的条目中.这是正常的做法.