簡體   English   中英

gradle構建后無法從jar中的資源加載文件

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

我的gradle構建腳本:

apply plugin: 'java'

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

repositories {
   mavenCentral()
}


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

我的文件夾結構:

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

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();
            }
        }
    }
}

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)

提取的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

在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