The program I'm currently working on are depending on reading and writing text files. My streams are working excellent in eclipse, but when I export the program it won't find the text files. I've also tried putting the jar file in a folder with the same filename paths, but it still does not work. I've tried many different classloaders, getResourceAsStream etc., but I cannot get it to work.
So, I need some way to retrieve the files outside eclipse. Hopefully, you'll get the idea of what I'm trying to do below:
You didn't say whether you have a package declaration in your classes. If you have a package declaration, the .class files will be inside directories with the same names as the parts of the package name. Those directories will be in another directory. Try copying the .txt files into that latter directory first, then into the child directories.
If you don't have a package declaration, try putting the .txt files, the .java files and the .class files all in the same directory.
Why are you using ObjectInputStreams etc, rather than Readers, for reading and writing text files? Are you closing the Streams? You can find examples of Readers and Writers in the Java™ Tutorials (look where it says "Buffered"). You will find Scanner and Formatter (if you don't need to append to the file) easier to use.
posted 11 years ago
Thanks for reply.
To answer your question, I use ObjectOutputStream and ObjectInputStreams because the textfiles store a HashMap, and the program imports the HashMap that is stored in the textfile.
The program works excellent, all I need to figure out is how the textfiles can be read in an exported jar file. When I export the program to an executable .jar file, the program wont find the textfiles. Any idea on how to solve it?
Well, first of all an entry in a jar file, executable or otherwise, can't be accessed as a File. It isn't a file as far as the operating system is concerned, so that's that. Besides, a relative path in a File is always relative to the current working directory, not to the classpath.
Second, the classpath of an executable jar is only the jar itself, plus whatever else is mentioned in the Class-Path entry of the jar's manifest.
Third, you can't write to an entry in a jar file. Consider it to be read-only.
So, whatever you are trying to do (it isn't 100% clear to me), you should probably reconsider in light of those three facts.
posted 11 years ago
What I'm doing is that I store a HashMap in a textfile and read this HashMap whenever it's needed (ObjectInputStream.readObject etc.). Everything works perfectly in Eclipse, and I cannot see why I have to change it? I've found so many similar examples to what I'm doing on the internet. The only problem I have is that when I export the program to an executable jar file the program does not find the text files (which are located in my src folder in eclipse).
Does this make things clearer?
posted 11 years ago
I figured it out now. I just copied the text files from my project src folder to the same folder as the jar file is located.
However, is it a way to make the program get the text files without having the physical files in the same folder as the jar file? For example by a similar getClass.getResource() method etc.?
Magnus Kul wrote:However, is it a way to make the program get the text files without having the physical files in the same folder as the jar file?
Could you post your findings , please?
posted 9 years ago
Samuel Jason wrote: . . . Could you post your findings , please?
After tow years, probably not, I am afraid.
I don't think you can use getResource() or similar to find files outside the .jar or the folder the Java™ application is in. You can try a file chooser to navigate to different folders. Don't know whether that helps you.
Legend has it that if you rub the right tiny ad, a genie comes out.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop