We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,248 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to include txt into jar file?

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?

7
Contributors
11
Replies
6 Years
Discussion Span
7 Months Ago
Last Updated
12
Views
Question
Answered
peter_budo
Code tags enforcer
Moderator
15,789 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50

you can't write to a file inside a jar.

jwenting
duckman
Team Colleague
8,522 posts since Nov 2004
Reputation Points: 1,656
Solved Threads: 345
Skill Endorsements: 19

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?

peter_budo
Code tags enforcer
Moderator
15,789 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50

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?

peter_budo
Code tags enforcer
Moderator
15,789 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50

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

peter_budo
Code tags enforcer
Moderator
15,789 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50

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?

purus
Newbie Poster
1 post since Apr 2006
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

try Class.getResourceAsStream to return the content as an InputStream.

jwenting
duckman
Team Colleague
8,522 posts since Nov 2004
Reputation Points: 1,656
Solved Threads: 345
Skill Endorsements: 19

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

Jayzaa
Newbie Poster
1 post since Dec 2006
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

Declan123
Newbie Poster
9 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 6 Years Ago by jwenting, purus, Declan123 and 1 other

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
Code tags enforcer
Moderator
15,789 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50

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

instead
Newbie Poster
1 post since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

peter_budo
Code tags enforcer
Moderator
15,789 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0874 seconds using 2.68MB