【问题标题】:Load resource file from outside of JAR file从 JAR 文件外部加载资源文件
【发布时间】:2015-05-10 13:55:29
【问题描述】:

使用maven,我正在构建一个必须动态加载驱动程序的应用程序。使用以下代码,它仅在 driver.so 位于 inside 生成的 JAR 文件时才有效。我该怎么做才能在路径./natives/driver.so 内的JAR外部 找到该文件。

package com.myproject;

public class Starter {
    public static void main(String[] args) {
        File classpathRoot = new File(Starter.class.getClassLoader().getResource("driver.so").getPath());
        System.out.println(classpathRoot);
    }
}

驱动程序位于inside时的输出JAR是:

jar:file:/home/ted/java/myproject/target/myproject-0.1-SNAPSHOT.jar!/libgdx64.so

定位外部 JAR(在targettarget/natives 目录中)时的输出是:

null

我通过以下方式启动应用程序:

cd /home/ted/java/myproject/target/
java -Djava.library.path=./natives -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter

我能做什么?

【问题讨论】:

    标签: java maven jar shared-libraries


    【解决方案1】:

    试试这个:

    package com.myproject;
    
    public class Starter {
        public static void main(String[] args) {
            File file = new File("natives/driver.so");
            System.out.println(file);
        }
    }
    

    或者这个:

    package com.myproject;
    
    public class Starter {
        public static void main(String[] args) {
            File file = new File(System.getProperty("java.library.path"), "driver.so");
            System.out.println(file);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 2012-12-06
      • 2013-08-02
      • 2018-06-03
      相关资源
      最近更新 更多