Sign in to follow this  
Nicholas Kong

Java Jar

Recommended Posts

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. 

 

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this