It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Project distribution jar file issue. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Project distribution jar file issue." Watch "Project distribution jar file issue." New topic
Author

Project distribution jar file issue.

Chuck Barnes
Ranch Hand

Joined: Aug 10, 2010
Posts: 37
I have a project that I want to distribute for testing.

I have netbeans compile the jar file for me, I zip up the dist dir with a simple run.cmd file to kick it off.
This project has an ini file included in the jar file. While in the IDE it runs fine as i have the path to the ini coded in to be found where ever the class is sitting.



So that works correctly with an 'exploded' file structure run from the IDE, but if I run it while compressed in a jar file from the command line it can't find the ini.

If i look at the contents of the jar i see the ini file where it should be. What am i doing wrong? Why can't it find the file?

fyi
The idea behind the ini file is it contains a URL and if it changes I want to just send a new ini file instead of new code.
Chuck Barnes
Ranch Hand

Joined: Aug 10, 2010
Posts: 37
upon looking around I found references to .properties files and even xml files to use when saving user settings to the user's home dir via the Properties class

But I don't see the similarity with my issue. my issue is with a static setting that I want to retrieve every time and user can't change it from the app.

Also, I didn't want stray files lying around. I thought that was the point to the jar file. everything in once place.

Also, whether we are talking about an ini file, a .properties file or an xml file. how does java know where to find said file inside the jar file? I would still have the same issue using any one of those file types, correct?
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
I think you need to make a manifest file with the jar file, I could be wrong though. I usually don't include external resources like images and configuration files in my jar file
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Chuck Barnes wrote:Also, whether we are talking about an ini file, a .properties file or an xml file. how does java know where to find said file inside the jar file? I would still have the same issue using any one of those file types, correct?


It doesn't, of course. You have to tell it by writing suitable code.

But the code you posted originally used a FileInputStream, which can't work because a resource inside a jar isn't a file. If I interpret what you posted there, it looks like the "setting.ini" file is in the same folder as the class named "read" inside the jar. In that case the code you want is this:


And yes, the last part of the file name has no effect on that.
Chuck Barnes
Ranch Hand

Joined: Aug 10, 2010
Posts: 37
Unnar Björnsson wrote:I think you need to make a manifest file with the jar file, I could be wrong though. I usually don't include external resources like images and configuration files in my jar file


What do you do with them? How do you package it along with the jar?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The purpose of the manifest is to identify where to find things which are outside the jar, at least that's what the Class-Path entry is for. Your problem is the opposite, namely how to find things which are inside the jar.
Chuck Barnes
Ranch Hand

Joined: Aug 10, 2010
Posts: 37
Paul Clapham wrote:
Chuck Barnes wrote:Also, whether we are talking about an ini file, a .properties file or an xml file. how does java know where to find said file inside the jar file? I would still have the same issue using any one of those file types, correct?


It doesn't, of course. You have to tell it by writing suitable code.

But the code you posted originally used a FileInputStream, which can't work because a resource inside a jar isn't a file. If I interpret what you posted there, it looks like the "setting.ini" file is in the same folder as the class named "read" inside the jar. In that case the code you want is this:


And yes, the last part of the file name has no effect on that.


Yes you are correct. that worked! Wonderful, thanks for the help

....but, i think I don't want to do it that way either..... LMAO. After I thought it about some more, I asked myself, do I really want to be sending out the jar file if only the settings file changes? well no... I only want to send the ini file itself and overwrite the old one.
That is the least amount of steps.

So i think i want it external to the jar, so is that where the manifest comes into play? how is that done? Is it something I can tell the IDE to construct? ie tell it to include the ini file in the dist folder, and in turn the manifest file would reflect that relative path? or is it a manual process?

at least I will know how to package things both in and outside of jars.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Chuck Barnes wrote:After I thought it about some more, I asked myself, do I really want to be sending out the jar file if only the settings file changes? well no... I only want to send the ini file itself and overwrite the old one.

On the other hand if you're distributing the jar file via JNLP then generating a new jar is by far the simplest way to do it, even if there's only one trivial change in the new jar.

And if you're distributing the jar by e-mailing it to your friends, or some similar process, then sending a new version of the jar is again the simplest way to do it. Otherwise you're going to need an installer which puts the jar and all of the pieces into the right place the first time, and a patcher which puts the revised pieces into the right place the next time.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Project distribution jar file issue.
 
Similar Threads
EclipseME: How to run a downloaded jar & jad combo?
Reading files located in runme.jar
Relative path with JAR files
Problem with Project references in ECLIPSE IDE
Run jar file from eclipse