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.

[485 byte] By [andrewnguyena] at [2007-11-27 6:03:37]
# 1

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.

DrClapa at 2007-7-12 16:46:36 > top of Java-index,Java Essentials,Java Programming...
# 2
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?
andrewnguyena at 2007-7-12 16:46:36 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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.

cotton.ma at 2007-7-12 16:46:36 > top of Java-index,Java Essentials,Java Programming...
# 4
Okay. Hint: if you need to update a file, don't put it in a jar.
DrClapa at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 5
Got data?1. Write to a file *outside* your application's jar file.2. Or use a database.
Hippolytea at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 6
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.
cotton.ma at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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.

cotton.ma at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 8
The answer is.... you can. Well that's sort of interesting....
cotton.ma at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 9

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....

cotton.ma at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 10
> > It actually works. I wonder if the classloader is> borked though....Nope.
cotton.ma at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 11
Thank you, guys. I'll try other way
andrewnguyena at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 12
Thank you cotton.
andrewnguyena at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 13

> 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.

cotton.ma at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 14
> > > > 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.
kevjavaa at 2007-7-12 16:46:37 > top of Java-index,Java Essentials,Java Programming...
# 15
Hi cotton.mIn case I have many different data files, how can the program read from the jar file?
andrewnguyena at 2007-7-21 21:41:15 > top of Java-index,Java Essentials,Java Programming...
# 16
> 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?
Hippolytea at 2007-7-21 21:41:15 > top of Java-index,Java Essentials,Java Programming...
# 17
I create a jar file which included all data files in txt. How the program can find specific data file?
andrewnguyena at 2007-7-21 21:41:15 > top of Java-index,Java Essentials,Java Programming...
# 18
Didn't reply #1 answer that question?
Hippolytea at 2007-7-21 21:41:15 > top of Java-index,Java Essentials,Java Programming...
# 19
> 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
cotton.ma at 2007-7-21 21:41:15 > top of Java-index,Java Essentials,Java Programming...
# 20
> Please see http://forum.java.sun.com/thread.jspa?threadID=5178617&start=1Thank you, Dr. Mbius.
Hippolytea at 2007-7-21 21:41:15 > top of Java-index,Java Essentials,Java Programming...
# 21

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

...

kevjavaa at 2007-7-21 21:41:15 > top of Java-index,Java Essentials,Java Programming...