• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read file from Jar - Exception

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Nicolas Kal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Nicolas Kal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nicolas Kal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Nicolas Kal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic