?
Solved

jar files

Posted on 2005-05-17
18
Medium Priority
?
365 Views
Last Modified: 2013-11-23
I have a class which is part of a package that I have thrown into a jar file.  This class needs to read a text file.  What is the best way to reference this text file from the class in this situation without hardcoding the absolute path of the text file?
0
Comment
Question by:rnicholus
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021062
Well the path to the text file is not exactly arbitrary, but you'll get most leeway doing something like


InputStream in = YourClass.class.getResourceAsStream("yourfile.txt");
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 14021078
Hi liamgannon,

put the file path/name into a property file on the root of ur pakage for example named (Parameters.properties) and in this file write:

FilePath=c:\\pathtofile

in ur code:

import java.util.*;

    ResourceBundle resourceBundle = ResourceBundle.getBundle( "Parameters" );
    String filePath = resourceBundle.getString( "FilePath" );

Cheers!
0
 

Author Comment

by:rnicholus
ID: 14021080
Can you explain what this does exactly?  That is, where does my text file need to be located for this to work?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:rnicholus
ID: 14021151
thanks, I will try that.  One more semi-related question...

In my source files, I specify a packagename, for example "package something;".  When I compile the source file "javac somefile.java" the package seems to be ignored.  That is, the class file is not created in the appropriate directory.  Why is this happening and how can I "fix" this?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021154
>>put the file path/name into a property file on the root ...

That just adds a layer of indirection to an absolute path ;-)
0
 

Author Comment

by:rnicholus
ID: 14021162
arrgh!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021171
>>Why is this happening and how can I "fix" this?

You need to put it in the appropriate directory or do

javac -d . YourFile.java
0
 

Author Comment

by:rnicholus
ID: 14021184
that fixed it.  Thanks!  

Now, back to my original question.  Can anyone agree on an approach?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021197
Well i think we can agree that putting an absolute path somewhere else is only marginally better than placing directly in your code ;-)
0
 

Author Comment

by:rnicholus
ID: 14030610
So, is there any way to do this without using an abs path?  If so, please explain.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 14030879
If you do

InputStream in = YourClass.class.getResourceAsStream("/yourfile.txt");

if will be loaded from the root of any path in the classpath
0
 
LVL 1

Expert Comment

by:hellsd
ID: 14030992
CEHJ is right,
 but you must not forget to handle it nicely with try, catch blocks...
0
 

Author Comment

by:rnicholus
ID: 14031195
Excellent.  I've once again learned some new things about Java.  Thanks everyone, especially CEHJ, who seems to end up answering almost all of my questions.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14031210
:-)
0
 

Author Comment

by:rnicholus
ID: 14031344
Sorry, one more small related question...

If add, say, C:\somedir to my class path, will Java automatically look through all subdirectories of that somedir directory as well when search the classpath?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14031401
No it won't recurse subdirectories. You'd have to name any you're interested in
0
 

Author Comment

by:rnicholus
ID: 14031460
Thank you, once again.  

It occurred to me that this may be a nice feature to be considered for future editions of Java.  I imagine that this feature could be implemented such that the JVM or compiler could be coded to look for options after each path in the classpath.  For example:

classpath = C:\test;C:\blah -r

Where the JVM/compiler would only look in the root test directory, but would recurse subdirectories from the root of blah.  

Just my 2 cents.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14031767
You could implement that yourself if you wanted by overriding findResource in ClassLoader
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question