I'm creating a jar file of my game to make it smaller and easier to transfer. I was able to created jar file out of my classes and include there images.

Question is - how do I include txt file which is used for reading and writing?

OK, I will leave rewritable out, but still there is one more txt file which is design for reading only. How do I add to the JAR?

Well, problem gets deeper, will try explain what I got...

I have work folder for my school project, there is a program which holds main function, then there is txt file which I will rewrite with new datas with each run of program. In same diredtory is folder with images, folder with other text files and package folder and manifest.

Manifest is game.mf

Main-Class: Game

In order to create JAR file I used following command

jar cmf game.mf Game.jar *.class GamePackage/*.class Image/*.gif TextFile/*.txt

It created for me, jar with all stuff inside but it is only working if I keep it in original directory. If I move game.jar and "rewritable" txt to diferent directory my images get lost and the function which use other txt file will not work(close down application)

any suggestions?

To anybody who wish to include images or read text file in their JAR fils here is small help

Include image

URL urlImage = getClass().getResource("Image/imageName.gif");
myImage = Toolkit.getDefaultToolkit().getImage(urlImage);

Include text file

JarFile jarFile = new JarFile("MyJar.jar");
JarEntry entry = jarFile.getJarEntry("TextFile/myTextFile.txt");
InputStreamReader input = new InputStreamReader(jarFile.getInputStream(entry) );

Don't forget include requered libraries :D

I am having the same problem reading a text file when my program gets packaged as a jar. The weird thing is that it has no problem finding a jpg image, it just can't find one txt file that is in the package. Do I really have to completely modify how I access the text file to get it to work in the jar package? Will it still work like normal too, or is this only for the jar package?

1. You can open the jar file using winrar
2. Create you txt file
3. Then drag it in
4. Click ok to compress

OK, I will leave rewritable out, but still there is one more txt file which is design for reading only. How do I add to the JAR?

I'm not sure if this what you're looking for, but you can use the command prompt and jar exe in the sdk.

Open command prompt and change the directory to where your class files are. Then type

C:\j2sdk1.4.2_13\bin\jar cvf jarFileName.jar classFileName(s).class textFileName.txt

(or whaterever version of the sdk you are using. i.e 1.5.0...)

if you want to make it executable then use

xvf jarFileName.jar

to extract it, the will create a directory call META-INF, in it will be a manifest file, open this file and on the third line, type

Main-Class: yourMainClass.class

Note: there must be a space after Main-Class:
Note: your main class is the class with the main method in it

Then use:

cvfm jarFileName.jar META-INF\MANIFEST.MF classFileName(s).class textFileName.txt

Hope this helps

ehh don't want to be rude guys but this problem is all long sorted if you read whole topic you may find I posted solution over there plus small tip

peter budo, U gave a solution to read text file from a jar. but he asked how to read it inside the code(jar).!!

Yeah and you just reopened 5 years old thread, didn't read it all as original poster was me back at uni and then my post was hijacked few times. So yeah, I got my answer and other like you didn't bother to read it all

This question has already been answered. Start a new discussion instead.