Problem for reading data file in jar file
I use eclipse to export my project to jar file. My project is a program used to update data of the company. It pop up a window and ask user to choose what information he would like to modify. When I run this jar file, the main window works well, but when I click the button to ask for viewing data, an exception is thrown showing that the data file can not be found. When exporting, I already include data files into the jar file.
Can any one help me please?
Thanks.
Presumably you have some code that is looking for files. But you don't have files in your jar, you have resources. So change that code to look more like this:InputStream is = this.getClass().getResourceAsStream("/thingy.txt");
Hopefully you don't plan to update these resources in your jar, as you won't be able to do that.
Hi,Thank you for your reply. Because my program is used to update data, then after modification, the data should be written to the file. Can you give me any hint?
> Hi,
>
> Thank you for your reply. Because my program is used
> to update data, then after modification, the data
> should be written to the file. Can you give me any
> hint?
Don't do that?
Do something else?
Get new requirements?
You can't just plop stuff into a Jar file. I mean technically it is possible but will require that you unjar everything and then rejar everything. Which would be... painful.
Okay. Hint: if you need to update a file, don't put it in a jar.
Got data?1. Write to a file *outside* your application's jar file.2. Or use a database.
Just as a mild point of interest. Is it possible to rejar the jar you are executing from? Or does the VM have a lock on this? I know how to unjar a jar file programmatically so I know that that part is technically possible.
> Just as a mild point of interest. Is it possible to
> rejar the jar you are executing from? Or does the VM
> have a lock on this?
>
> I know how to unjar a jar file programmatically so I
> know that that part is technically possible.
Although I know you can delete class files that are in use. So maybe you can.
Hmmm I may have to test and see.
The answer is.... you can. Well that's sort of interesting....
import java.io.*;
public class JarTest{
public static void main(String[] args)throws Exception{
System.out.println("Starting deviousness...");
File f = new File("myjar.jar");
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[(int)f.length()];
in.read(buffer);
in.close();
FileOutputStream out = new FileOutputStream(f);
out.write(buffer);
out.close();
System.out.println("Finished!");
}
}
Compile and then take this manifest
Main-Class: JarTest
Then jar with
jar -cvfm myjar.jar manifest JarTest.class
and run
java -jar myjar.jar
It actually works. I wonder if the classloader is borked though....
> > It actually works. I wonder if the classloader is> borked though....Nope.
Thank you, guys. I'll try other way
> Thank you cotton.
You're welcome. I was curious what would happen. I am a bit surprised it works as well as it does. I am not actually unjarring and rejarring but that's relatively minor.
It can be done. I guess. I still wouldn't and I suspect there are some large gotchas in places.
> > > > It actually works. I wonder if the classloader is> > borked though....> > Nope.Sweet. One more step toward my self-healing, self-aware, and self-compiling Java Application that will someday take over the world.
Hi cotton.mIn case I have many different data files, how can the program read from the jar file?
> In case I have many different data files, how can the program read from the jar file?You say you know how to read from one file, but you want to be able toread from more that one file. What's the problem? Or the real question?
I create a jar file which included all data files in txt. How the program can find specific data file?
Didn't reply #1 answer that question?
> I create a jar file which included all data files in> txt. How the program can find specific data file?Please see http://forum.java.sun.com/thread.jspa?threadID=5178617&start=1
> Please see http://forum.java.sun.com/thread.jspa?threadID=5178617&start=1Thank you, Dr. Mbius.
> > I create a jar file which included all data files
> in
> > txt. How the program can find specific data file?
>
> Please see
> http://forum.java.sun.com/thread.jspa?threadID=5178617
> &start=1
Exception in thread "kev" java.lang.StackOverflowError
at kev.followLink(kev.java:2473)
at kev.followLink(kev.java:2473)
at kev.followLink(kev.java:2473)
at kev.followLink(kev.java:2473)
...