简体   繁体   English

gradle构建后无法从jar中的资源加载文件

[英]After gradle build can`t load file from resource in jar

my gradle build script: 我的gradle构建脚本:

apply plugin: 'java'

jar {
    manifest {
        attributes 'Main-Class': 'com.package.Starter'
    }
}

repositories {
   mavenCentral()
}


dependencies {
   compile 'javax.persistence:persistence-api:1.0.2'
}

my folder structure: 我的文件夹结构:

└── main
├── java
│   └── com
│       └── packege
│           ├── pojo
│           │   ├── City.java
│           │   └── GeocodeResponse.java
│           │   
│           └── Starter.java
│       
└── resources
    └── cities.csv

java function in Starter.java Starter.java中的Java函数

private void set_cities(){
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ";";
    URL url = Starter.class.getResource("cities.csv");

    // print class folder for debug
    ClassLoader cl = ClassLoader.getSystemClassLoader();

    URL[] urls = ((URLClassLoader)cl).getURLs();

    for(URL url1: urls){
        System.out.println(url1.getFile());
    }

    try {

        br = new BufferedReader(new FileReader(url.getFile())); // <-- null :(((
        while ((line = br.readLine()) != null) {
            String[] data = line.split(cvsSplitBy);
            cities.add(new City(data[1], data[0]));
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

After gradle build I want to start from jar file from /build/libs/h.jar using java -jar h.jar command but output is: gradle构建后,我想使用java -jar h.jar命令从/build/libs/h.jar从jar文件开始,但输出为:

/path/to/project/h/build/libs/h.jar // <-- output of URL[] urls = ((URLClassLoader)cl).getURLs();
Exception in thread "main" java.lang.NullPointerException
at com.package.Starter.set_cities(Starter.java:90)
at com.package.Starter.<init>(Starter.java:34)
at com.package.Starter.main(Starter.java:133)

extracted jar structure: 提取的jar结构:

├── cities.csv
├── com
│   └── package
│       ├── pojo
│       │   ├── City.class
│       │   ├── GeocodeResponse.class
│       │   ├── GeocodeResponse$Result$AddressComponent.class
│       │   ├── GeocodeResponse$Result.class
│       │   ├── GeocodeResponse$Result$Geometry$Bounds.class
│       │   ├── GeocodeResponse$Result$Geometry$Bounds$Northeast.class
│       │   ├── GeocodeResponse$Result$Geometry$Bounds$Southwest.class
│       │   ├── GeocodeResponse$Result$Geometry.class
│       │   ├── GeocodeResponse$Result$Geometry$Location.class
│       │   ├── GeocodeResponse$Result$Geometry$Viewport.class
│       │   ├── GeocodeResponse$Result$Geometry$Viewport$Northeast.class
│       │   └── GeocodeResponse$Result$Geometry$Viewport$Southwest.class
│       └── Starter.class
└── META-INF
    └── MANIFEST.MF

after gradle build comand gradle create build folder: 在gradle build comand gradle create build文件夹之后:

├── classes
│   └── main
│       └── com
│           └── package
│               ├── pojo
│               │   ├── City.class
│               │   ├── GeocodeResponse.class
│               │   ├── GeocodeResponse$Result$AddressComponent.class
│               │   ├── GeocodeResponse$Result.class
│               │   ├── GeocodeResponse$Result$Geometry$Bounds.class
│               │   ├── GeocodeResponse$Result$Geometry$Bounds$Northeast.class
│               │   ├── GeocodeResponse$Result$Geometry$Bounds$Southwest.class
│               │   ├── GeocodeResponse$Result$Geometry.class
│               │   ├── GeocodeResponse$Result$Geometry$Location.class
│               │   ├── GeocodeResponse$Result$Geometry$Viewport.class
│               │   ├── GeocodeResponse$Result$Geometry$Viewport$Northeast.class
│               │   └── GeocodeResponse$Result$Geometry$Viewport$Southwest.class
│               └── Starter.class
├── com
│   └── package
│       ├── pojo
│       │   ├── City.class
│       │   ├── City.java~
│       │   ├── GeocodeResponse.class
│       │   ├── GeocodeResponse$Result$AddressComponent.class
│       │   ├── GeocodeResponse$Result.class
│       │   ├── GeocodeResponse$Result$Geometry$Bounds.class
│       │   ├── GeocodeResponse$Result$Geometry$Bounds$Northeast.class
│       │   ├── GeocodeResponse$Result$Geometry$Bounds$Southwest.class
│       │   ├── GeocodeResponse$Result$Geometry.class
│       │   ├── GeocodeResponse$Result$Geometry$Location.class
│       │   ├── GeocodeResponse$Result$Geometry$Viewport.class
│       │   ├── GeocodeResponse$Result$Geometry$Viewport$Northeast.class
│       │   └── GeocodeResponse$Result$Geometry$Viewport$Southwest.class
│       ├── Starter.class
│       └── Starter.java~
├── dependency-cache
├── libs
│   ├── h <-- extracted jar
│   │   ├── cities.csv
│   │   ├── com
│   │   │   └── package
│   │   │       ├── pojo
│   │   │       │   ├── City.class
│   │   │       │   ├── GeocodeResponse.class
│   │   │       │   ├── GeocodeResponse$Result$AddressComponent.class
│   │   │       │   ├── GeocodeResponse$Result.class
│   │   │       │   ├── GeocodeResponse$Result$Geometry$Bounds.class
│   │   │       │   ├── GeocodeResponse$Result$Geometry$Bounds$Northeast.class
│   │   │       │   ├── GeocodeResponse$Result$Geometry$Bounds$Southwest.class
│   │   │       │   ├── GeocodeResponse$Result$Geometry.class
│   │   │       │   ├── GeocodeResponse$Result$Geometry$Location.class
│   │   │       │   ├── GeocodeResponse$Result$Geometry$Viewport.class
│   │   │       │   ├── GeocodeResponse$Result$Geometry$Viewport$Northeast.class
│   │   │       │   └── GeocodeResponse$Result$Geometry$Viewport$Southwest.class
│   │   │       └── Starter.class
│   │   └── META-INF
│   │       └── MANIFEST.MF
│   └── h.jar
├── resources
│   └── main
│       └── cities.csv
│       
└── tmp
    ├── compileJava
    └── jar
        └── MANIFEST.MF

您可以使用资源文件的绝对路径以及getResourceAsStream方法来访问您的jar,在这种情况下:

Starter.class.getResourceAsStream("/cities.csv");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM