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?
peter_budo
Code tags enforcer
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
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
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
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
15,789 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50
try Class.getResourceAsStream to return the content as an InputStream.
jwenting
duckman
8,522 posts since Nov 2004
Reputation Points: 1,656
Solved Threads: 345
Skill Endorsements: 19
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
15,789 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50
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
15,789 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50