我正在使用 gradle build 构建 uber jar,我使用的插件是 https://github.com/johnrengelman/shadow
我的文件src/main/resources
夹中有一个 json 文件,我在下面的代码中使用它
public Repository<Product> initializeData() {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("product.json").getFile());
System.out.println(file.getAbsolutePath());
Repository<Product> productRepository = new ProductRepository();
return productRepository;
}
当我运行 uber Jar 时,面临以下问题
$$ java -cp libs/checkout-service-1.0-SNAPSHOT.jar com.noths.runner.Runner
java.nio.file.NoSuchFileException: /home/gradle/checkout-service/file:/home/gradle/checkout-service/libs/checkout-service-1.0-SNAPSHOT.jar!/product.json
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:219)
at java.base/java.nio.file.Files.newByteChannel(Files.java:371)
at java.base/java.nio.file.Files.newByteChannel(Files.java:422)
at java.base/java.nio.file.Files.readAllBytes(Files.java:3206)
at java.base/java.nio.file.Files.readString(Files.java:3284)
at java.base/java.nio.file.Files.readString(Files.java:3243)
at com.noths.runner.Utils.initializeData(Utils.java:21)
at com.noths.runner.Runner.main(Runner.java:21)
Exception in thread "main" java.lang.NullPointerException
at com.noths.promotions.ProductPromotion.apply(ProductPromotion.java:26)
at com.noths.promotions.DefaultPromotionsRunner.setPromotionsChain(DefaultPromotionsRunner.java:21)
at com.noths.checkout.CheckoutServiceImpl.checkout(CheckoutServiceImpl.java:27)
at com.noths.runner.Runner.main(Runner.java:25)
我的 build.gradle 看起来像这样
// Shadow Plugin https://github.com/johnrengelman/shadow
plugins {
id "com.github.johnrengelman.shadow" version "6.0.0"
id 'java'
}
group 'com'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
testCompileOnly 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
implementation 'com.google.code.gson:gson:2.8.8'
}
test {
useJUnitPlatform()
}