Java Jar

Started by
5 comments, last by LorenzoGatti 10 years, 8 months ago

So I created Java jar files in the past. But they will not execute unless the jar file is with the src folder and the manifest specifices the entry point of the program.

The reason it will not execute is because my code looks for the folder called src and then look for specific file path so it can read certain files within the src directory.

This forces me to execute the jar which produces the folder the program will need to do its job.

The only problem I found is that my total size of the program is twice as large because of this.

size of jar: 35 mb

src folder: 39.2 mb

The reason I ask is because I remember trying other people jar and all that is required for the program to run is the jar itself.

Advertisement
If you need additional resources then those have to be added to the Jar as well and you need to load them properly. Most likely that will mean using Class.getResourceAsStream or a similar function. How you add the resources in the first place will largely be a question of the build system you are using.

If you need additional resources then those have to be added to the Jar as well and you need to load them properly. Most likely that will mean using Class.getResourceAsStream or a similar function. How you add the resources in the first place will largely be a question of the build system you are using.

I am using Eclipse. You can add things to a Jar. From my experience, it was more about creating a jar from the .java files and resources I made.

Don't use the src folder for your resources. src is for the sources (.java). Add another folder to the project and store your resources there.

If you package your resources inside the .jar, I think you'd need to load them as BitMaster said. FileInputStream works with the files relative to the .jar rather than the files inside the jar itself.

You don't need to pack the .java inside the jar. When you compile to a executable .jar, .java files get compiled to .class, and those are the ones the VM runs.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Don't use the src folder for your resources. src is for the sources (.java). Add another folder to the project and store your resources there.

If you package your resources inside the .jar, I think you'd need to load them as BitMaster said. FileInputStream works with the files relative to the .jar rather than the files inside the jar itself.

You don't need to pack the .java inside the jar. When you compile to a executable .jar, .java files get compiled to .class, and those are the ones the VM runs.

Okay I will try FileInputStream object and see what happens. Thanks guys!

I think you misunderstood.

FileInputStream -> For stuff outside the jar (ie, a file inside a folder).
getResourceAsStream() -> For stuff inside the jar.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

If you have files that you want to keep outside the jar file (main reasons: they can be modified or they can be usefully shared with other software), maybe you should use the preferences API to remember paths in the user's file system (you can ask the first time the application is run), and open FileInputStreams or the like from them.

This approach has the advantage of ignoring the differences between running/debugging your application from Eclipse and running it properly from a jar file and an "installation directory" containing your resources: in both cases files would be accessed at fixed arbitrary locations.

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement