aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Read file from Jar - Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Read file from Jar - Exception" Watch "Read file from Jar - Exception" New topic
Author

Read file from Jar - Exception

Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69
Hi to everyone!

I'm almost finished with my assignment and yesterday I came with an issue, regarding the "suncertify.properties" and the documentation which is available to the user inside the system. The application when launched through eclipse is running as it supposed, it checks the "suncertify.properties" for stored specification and the user documentation is loaded whenever the user requests it. I exported the jar file and the "suncertify.properties" file is in the root file but instead of reading that file it creates a new "suncertify.properties" file in the same directory that the jar relies. This is the code that reads successfully on development but not on production[jar]



Why on earth does it behaves like this and it does not use the existing file inside the jar ?

My second issue is again regarding file so that's why I include in the same section. For the application I developed uer quidance is provided through out the varius screens of the system. All user documentation is placed within a package with the following structure: /client/documentation
During development I ecountered many issues while trying to access files in that directory from "/client/swing/components" and came up with the following solution which is working on development but not on production



This "cheat" is working but not when the application is executed from within the jar. I tried the following solution but again I'm not able to read the file.



I've tried to modify the first line with the followings but again it can't read the file
FileInputStream fis = new FileInputStream("/client/documentation/"+fileName+ ".uig");
FileInputStream fis = new FileInputStream("client/documentation/"+fileName+ ".uig");
FileInputStream fis = new FileInputStream("client.documentation."+fileName+ ".uig");
FileInputStream fis = new FileInputStream("/client.documentation."+fileName+ ".uig");

Thanks in advance for your help


SCJP 6.0 , SBCD (Preparing..),
MSc Enterprise Systems Development, BSc Computer Science
Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69
My instructions specify the following

Your programs must not require use of command line arguments other than the single mode flag, which must be supported. Your programs must not require use of command line property specifications. All configuration must be done via a GUI, and must be persistent between runs of the program. Such configuration information must be stored in a file called suncertify.properties which must be located in the current working directory.


When it sais "must be located in the current working directory" i suppose that it means inside the jar because if the current working directory is the directory from where the runme.jar is executed then my first issue is solved.
Radi Hadzhiyski
Ranch Hand

Joined: Jun 30, 2009
Posts: 47
Hi, i think you don't have to load/store the properties from/to a file within the jar file.
It have to look just in the same directory, not in the jar file.

use:


or something like this.


SCJP 1.6 (91%), SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Hi Nicolas,

First of all welcome to the javaranch!

The working directory can be retrieved with this code:


You should not package the suncertify.properties file in your submission jar, nor the runme.jar. At first run it will be created (with some default values for example). Consecutive runs will then read that newly created file. That's how I implemented it.

Regarding your second query: I didn't add user documentation in my application because that's extra complexity which is not required. A userguide txt file is much easier. But I think your main problem is how to retrieve a file from a class, so here you can find some info about loading property and configuration files in Java. Maybe it is helpful to you.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69
Thanks both for the clarification,

I resolved my first issue, I missunderstood my requirements and thought that actually my "suncertify.property" file should be placed on the root folder inside the jar. I'm looking how it is possible to read the user guidance files inside the jar and i'm ready to submit
Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69
I have taken out the documentation from the source folder. Furthermore in my instruction it specifies the following :

A directory called docs, containing the following items at the top level:
..
User documentation for the database server and the gui client. If your user documentation is online then you may omit this file. However, if the documentation is not online, you must provide either a single plain ASCII (not word processor format) text document, which must be called userguide.txt, or multiple HTML files which must all be accessible from a starting point document that must be called userguide.html.


Since my user-guidance is online (built-in the application) i created a new folder "online-guide" and placed the help files for each screen there, i deployed the jar, executed the application and user guide is working now from the application.

Since all of these are going to be packaged in one jar, submision, i suppose that the submission jar will be extracted when it comes to evaluate my application otherwise it was pointless to remove the documentation and place it within the submission jar which holds all of the submision parts.
Any insights on this?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1963
    
    7

The submission jar consists of the executable jar (runme.jar), docs, code, version.txt and the "original" database file. Sun examiners extract the submission jar before running the app.

About your help, you say built-in to the app as in you got a help menu item and pop up a window with the help contents? The userguide stuff can be online or offline or in-app. Online is accessible using "http://...."; offiline is just from the docs directory and in-app is what I have described earlier. Of course if in-app then you need to have those files inside runme.jar file.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69
My user guidance is supported within the application. I don't have a menu "HELP" but there is help available throughout the application lifecycle. Each view has a generic function which locates its help file and present it to the user. I don't how you implemented your's user guidance but I didn't want to include large pieces of Strings in my code. If i include them inside the jar then I can't access those file. If you have used files to read the user guidance and placed them inside your runme.jar how did you accessed them from your application?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1963
    
    7

Nicolas Kal wrote:If you have used files to read the user guidance and placed them inside your runme.jar how did you accessed them from your application?


Ah I remember trying that when I did my assignment and I recall you need to use ClassLoader.getResourceAsStream stuff. Of course I ended up just provided offline because I recall I wasn't able to do something with it. Oh and the path of those files (eg inside some package or at the root of the jar) makes retrieving the files tricky.
Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69
Eureka !!

Finally my problems are solved!

I managed to access and read the files within the jar file and not in the same package with the classes or the root folder. I supply the code

InputStream in = ClassLoader.getSystemResourceAsStream("client/docs/"+getController().getClass().getSuperclass().getSimpleName()+ ".uig");



Now when you run on production you have to set the set the class path to you jar file


Then all i have to do is run my application and user guidance is fully working! Thanks for your assistance and good luck to all of us who haven't submitted !
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1963
    
    7

Not sure if setting the classpath is good when you submit. Would Sun examiners actually set their classpath before running your program? Besides you can't have -cp flag when running app - just simply "java -jar runme.jar <parm>".

And if they can't run your app from the getgo ... you fail. So you have been warned.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Hi Nicolas,

You are providing context-sensitive help and I think the hardest part is the one you are having trouble with at this moment: localizing your help files through getResourceAsStream (and other similar methods). If I were you I would just skip all this and just make a userguide.txt (or userguide.html if you want some images to be added). It's very easy, you don't have any issues with this approach and you don't lose any valuable time.

Maybe you can have a look at the sources of JavaHelp, some api to add context-sensitive help. Maybe you can find a method to read your help files in a consistent way.

Good luck!
Kind regards,
Roel
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Nicolas,

From instructions.html:
Your programs must not require use of command line arguments other than the single mode flag, which must be supported. Your programs must not require use of command line property specifications.

So your workaround conflicts with a must, which could mean automatic failure...

Kind regards,
Roel
Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69
I moved the runme.jar to my D: driver (not c: where it was) and there the classpath is not set. I didn't set the classpath to "D:runme.jar" just run the application and it loaded without any errors and the user guidance is loaded. I'm almost finished i'd rather not change it in the last minute, i'm also looking at tutorial at how you can modify the MANIFEST and introduce there properties but since it is working without having to set the classpath i'll stay with this approach.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2246

Howdy, Nicolas!

Champion, one way to easily access resources you have in your class path is this:



Where "myResource" indicates the fully qualified name of your resource. The URL object will give you several options to deal with the resource. Maybe this will be helpful to you!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Nicolas Kal
Ranch Hand

Joined: Sep 09, 2009
Posts: 69
Hi Roberto,

Thanks for the reply, i got it working with the files inside the jar, i'll finalize the rest of details and i am ready to submit,

Thanks all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read file from Jar - Exception
 
Similar Threads
Error printing out from a read in file.
File path issues while reading an xsl file in java
to read lines inside brackets
Reading one line from a text
problem while dispalying from file