Loading files from Resource folder using java jar - java

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

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

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

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.

Related

Running jar from command line gives FileNotFoundException

This is the code I am running in Java project to read from a text file:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("wordsEn.txt").getFile());
BufferedReader br = new BufferedReader(new FileReader(file);
When I run this program in Intellij IDEA, everything works fine, but when i build its JAR file, put it in my desktop, and run it through command line from my desktop with the command java -jar SyzygiesGudrat.jar it gives FileNotFoundException
Where do I have to put the text file in order to run this JAR from anywhere or in order someone else to be able to run this JAR when I send it to him?
My project structure looks like this:
Changed the code to the below one, as #MadProgrammer suggested. Now everything works fine.
InputStream in = this.getClass().getResourceAsStream("wordsEn.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in);
I stucked with the same issue.
Solution is : you can create a jar by IDE or command prompt.
while running a jar in your folder where you store a jar must give txt file.
For example, if you save jar on desktop, then on your desktop must have txt file before you run a jar from command prompt.

Reading text file from outside a JAR file?

I am able to read and alter a text file whilst in Eclipse, however when exported as a "Runnable JAR" it no longer works, it seems to me that the relative path is from inside the JAR, not to the JAR.
BufferedReader bufferedReader = new BufferedReader(new FileReader("RELATIVE PATH"));
I want it so that if I export my program as program.jar, and have file.txt in the same folder as program.jar, it opens file.txt if that is the relative path set.
Thanks for any help.
You can fetch the directory of your .jar file according to the solution of this answer and make your file accesses relative to that directory.
Beware of the warning concerning loading your class from a non-file location!

Include text file in Eclipse JAR export?

I am using a FileInputStream to read from a File object. My program is able to read the text file when run in the Eclipse IDE, but not when it is run as a JAR file.
I am exporting it as a JAR file and not a Runnable JAR file, and I know that the text file is already included in the JAR file because I extracted it to check.
This is what my file structure looks like:
I would suggest you to put your learnaboutfonts.txt inside src folder(not inside package) and read file in source code like the following:
Resource resource = new ClassPathXmlApplicationContext().getResource("classpath:learnaboutfonts.txt");
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
And you can use br as you want to use. Happy Coding.
You should use something like:
InputStream input = getClass().getResourceAsStream("/learnaboutfonts.txt");
Note that the trick is using the leading "/" properly. If you don't put the leading /, then it searches for the file in the package of the class its invoked from.
How do you access your file? You maybe need a relative file path like "./mytext.txt").
Use this pattern:
InputStream is = new getClass().getResourceAsStream(
"/text.txt");
Test to include your text-file in your package of your class where you want to access the file.

Property file not found in JAR file

I have a project which has Main method to invoke the application. Which is bundled in a JAR file.
I am trying to invoke the application using following line:
java -jar sample.jar -sample 123
This sample.jar bundled with config/config.properties in it.
I have following line of code to read the property file from the JAR.
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream("config/config.properties");
OR
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream("/config/config.properties");
This code is not able to find property file from the JAR file, While this property file is already exist in the JAR.
Does any one know the solution on this ?
When i moved config.properties out from the config Folder, it's working fine...
I am not sure why that's the case. But if i use following line of code it's working now..
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = loader.getResourceAsStream("config.properties");

Access the folder from jar file

I am trying to use a jar file which itself is a web application in another web project. In my jar which i have created using eclipse's export to jar functionality, I have stored owl files in a folder. To use relative paths in the code in the jar I access it using
MyClass.class.getResource("/folderName").getPath();
and this works fine when I deploy (glassfish) and use the project as a separate application. But when I am using the same from within another project, i changed the path like:
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getResourceAsStream("/folderName"), "UTF-8"));
ArrayList<File> filesFromCommonOntology = new ArrayList<File>();
while(str = br.readLine() != null)
{
File f = new File(line);
System.out.println(f);
filesFromCommonOntology.add(f);
}
for (int i = 0; i < filesFromCommonOntology.size(); i++)
{
file = filesFromCommonOntology.get(i);
String filePath = file.getPath();
Model timeModel = FileManager.get().loadModel(filePath,
ApplicationConstants.N3);
}
But when i run the project i get null pointer exception for line
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getResourceAsStream("/folderName"), "UTF-8"));
How to resolve this problem?
You cannot enumerate files in a folder when the application is packaged into jar. It is even difficult to imagine how this could work at all: you open a folder as an inptu stream and read the file names (in that folder) that way?
The simplest way would be to add a text file containing entries in the folder, probably with some descriptions. MyClass.class.getResourceAsStream should open files (not folders) from the jar.
If you have very huge number of files, you can also try to reopen own jar as ZipFile and then you can search for the entries in you folder using ZipFile API. However this is more complex.

Categories