Reputation: 307
I have download.sh file in my src/main/resource folder in maven project and I am reading it through below code
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("download.sh").getFile());
The file is reading when I run this as the standalone application.
If I run this as application using jar ex:-
java -jar runScript.jar SCHEMA_NAME
/Users/IdeaProjects/RunScript/target/file:/Users/IdeaProjects/RunScript/target/RunScripts.jar!/download.sh": error=2, No such file or directory
Can anyone help me in reading file from resource when executing with jar
Upvotes: 1
Views: 1260
Reputation: 311039
Resources are not files. You can get their URLs, or input streams, but nothing you can do will turn a resource in a JAR file into a file on the disk that you can use with File
. Short of unpacking the JAR file, that is.
Upvotes: 0
Reputation: 8491
I think this error happens when you try to read the file. This is because resources inside your jar are not files but streams. So you should use
getClass().getResourceAsStream("download.sh");
and then read that stream, for example:
InputStream resourceStream = getClass().getResourceAsStream("download.sh")
BufferedReader br = new BufferedReader(new InputStreamReader(resourceStream));
String line;
while ((line = br.readLine()) != null) {
// do something
System.out.println(line);
}
Upvotes: 1
Reputation: 703
I think you are not packaging your resources along with your jar. Please verify your jar contains the resource file (download.sh) with
jar -tvf <yourfile.jar> | grep -i download
Upvotes: 0