Reputation: 7852
my gradle build script:
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
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:
/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:
├── 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:
├── 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
Upvotes: 1
Views: 2435
Reputation: 2955
You could use absolute path to your resource file and, getResourceAsStream method to be able to access your jar, in this case:
Starter.class.getResourceAsStream("/cities.csv");
Upvotes: 2