gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:03:00 24 4
gpt4 key购买 nike

我的 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 命令和 gradle 创建构建文件夹之后:

├── 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");

关于java - gradle构建后无法从jar中的资源加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31605289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com