I have spent the past 3 nights going crazy trying to find an answer to this.
So I have a java program and I want it to be in a jar format and I want it to be able to read in text and image files.
I got the image files working fine using the this.getClass.getResource("") method, however I can not get the program to properly access the text files within the .jar, When I extract the jar, the text files are there so I know It is not a simple mistake of the text files not being within the jar
This is what I tried using, but it didn't work(It works without a jar, but now within a jar)
URL lurl = this.getClass().getResource("list.txt");
BufferedReader in3 = new BufferedReader(new FileReader(lurl.getFile()));
Fixes?
Assuming your class is
my.package.MyClass
The method will read the file from directory /my/package from JAR.
You can open the resource via:
BufferedReader in3 = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("list.txt")));
Related
I am making an application where I have to read text-files in order to make my program work. When I run this application in InteliJ it runs perfectly but when I make a .jar file I get a "no such file exception".
Can someone explain me how to access my files inside a .jar file from a class inside the same .jar file?
Can someone also tell me which type of path is more preferable to use (Absolute or relative)?
NOTE: the purpose of the program is to help friends to learn basic words in Arabic. This means that the .jar file will be moved from one computer to the other and it still has to work.
By the way, is putting everything into a .jar something good or are there better ways to put my project in one executable file?
Image of my directory structure
The code i use is:
Path path = Paths.get("src/Multimedia/Woordenschat/Boek1/Arabisch/Hoofdstuk 1.txt");
You can use a BufferedReader and get the file stream with getResourceAsStream():
InputStream in = getClass().getResourceAsStream("path");
BufferedReader input = new BufferedReader(new InputStreamReader(in));
I'm working on a Java program in Netbeans that I'd like to be able to
run from an external jar file
And I cannot seem to read from a file that isn't located inside of
the project default directory or some subfolder located there, and
that only works inside Net beans.
What I'd like to be able to do is read the text file from the file
path
src/assets/files/textFile.txt
.
I've tried all of the suggestions here, but they don't seem to work for me. Here's the code I'm currently using:
File file = new File("assets/files/textFile.txt");
if (!file.exists()) {
throw new FileNotFoundException("File does not exist");
}
FileReader fr = new FileReader(file.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);
When I try to run this, the exception is thrown each time.
The term for such non-Files is resource.
String encoding = "UTF-8";
new BufferedReader(
new InputStreamReader(YourClass.getResourceAsStream("/assets/files/textFile.txt"),
encoding));
Also FileReader is an old utility class that uses the default system encoding. That might be different from where the text file was created, hence Unicode, such as encoded in UTF-8, would be ideal. For Windows users you might write a BOM character at the beginning to mark the file as UTF-8: "\ufeff".
The class YourClass should be from the jar. Here an absolute path is used "/...". A relative path would start with the package path.
Also Windows is not case-sensitive. Other systems and the zip format of the jar are case-sensitive. Check the jar with a zip utility for correct paths.
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.
I know that there are many questions like this out there, but so far there have been none that have been of help.
In eclipse, I have a file inside of my project folder ,and I can get it to load using:
BufferedReader in = new BufferedReader(new FileReader(new File(path)));
When I export the project it will not load the file because it cannot find the file. I have no idea what is going on. Any suggestions? Thanks.
If you want to read the file, you have two options.
You Could...
Make sure that the file is in the same directory as the exported jar file and/or the same execution context. This will allow you to use something like BufferedReader in = new BufferedReader(new FileReader(new File("./" + fileName)));
When referencing the file, it need to specify a relative path to the file.
This means you must ensure that the file is copied to the correct location when ever you move the jar file.
You Could...
Embed the file within the jar as an embedded resource. This means that the file becomes part of the jar file. This also means that you can no longer reference it as a File, but need to use Class#getResource or Class#getResourceAsStream, for example...
BufferedReader in = new BufferedReader(
new InputStreamReader(this.getResourceAsStream("/path/to/resource")));
In order to use this, the file must be placed within the resources directory within your Eclipse project and the directory included within your build path
If you want to be able to write to the file, then you MUST use option one. Embedded resources can not be written to (without a lot of work)
I have a file holding default information that I use to load the textFields of my application. I looked up how to get this built into my jar file when I build and I was told to put it in the source packages and it would be brought along, so I have done that.
File Structure:
Project
-Source Packages
-src
~Java Classes
-defaultFiles
~Defaults.txt
The code I am trying to use is this:
BufferedReader in;
try {
URL resourceURL = FuelProperties.class.getResource("/defaultFiles/Defaults.txt");
in = new BufferedReader(new FileReader(resourceURL.getPath()));
}
And this works perfectly when I run it through NetBeans but when I build the project and try to run it from the jar file it is not grabbing the file.
I have verified that the default file is being built and exists in the same file structure shown above.
If you can help me out with this I would be extremely grateful as I have no idea what is keeping this from working. Thanks.
You have to lookup in the classpath, not on the disk.
The API to use is :
URL resourceURL : this.getClass().getResource("relative path in the classpath");
Once you have the url you can open a stream, etc.
EDIT : in the main method, you of course need to replace
this.getClass()
by
ClassName.class
I found the answer after searching through a couple dozen questions. It turns out that you can only get a InputStream of the data within a file within your JAR not a File object like I was attempting to do.
(If you want the File object you just have to extract the files from the JAR in your program and then you have access to it.)
So the code that got my problem to work was simply replacing this:
URL resourceURL = FuelProperties.class.getResource("/defaultFiles/Defaults.txt");
in = new BufferedReader(new FileReader(resourceURL.getPath()));
With this:
in = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/defaultFiles/Defaults.txt")));
And now it is working both inside NetBeans and in the Built JAR file.