代码之家  ›  专栏  ›  技术社区  ›  f1sh

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

  •  13
  • f1sh  · 技术社区  · 14 年前

    问题说明了一切。

    在我的例子中,特殊之处在于当前工作目录不是jar文件的位置,而是 c:\Windows\system32 (我的jar文件由windows使用右键单击菜单启动,我想将文件夹的路径作为参数传递给jar)。

    config.xml 外部

    我很难加载那个文件。Windows执行该行

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

    把整件事都说成 cmd /k 使windows命令提示符保持打开状态,以便可以看到jar的输出。

    new File(".") System.getProperty("user.dir") 这些函数返回时的相对路径 C:\Windows\system32\. C:\Windows\system32

    Launcher.class.getResourceAsStream("/../config.xml") 任何一个。因为这条路从 / ../config.xml 精确指向那个文件,但调用返回 null .

    有人能给我指出正确的方向吗?我真的被困在这里了。每次加载文件都让我很烦。。。

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

    除了最初的问题,我很难有maven2的地方 Class-Path: . 进入 MANIFEST.MF (BalusC发布的解决方案)使用时 jar-with-dependencies . 对于那些关心我是怎么做到的人:

    <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>
    
    2 回复  |  直到 14 年前
        1
  •  7
  •   BalusC    14 年前

    Launcher.class.getResourceAsStream("/../config.xml") Class-Path 罐子的入口 MANIFEST.MF 文件。这是正常的做法。

        2
  •  12
  •   Sean Patrick Floyd    14 年前

    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.
    }
    

    YourMainClass


    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.