Solved

Returning path to class file

Posted on 2001-06-19
4
164 Views
Last Modified: 2010-04-16
I want to be able to read a text file from the same directory where my Jar file resides, or class files if I've not yet packaged them up.

How do I return the path to the class file or JAR file if the class I'm in is inside a JAR file ?

Thanks
Dazza
0
Comment
Question by:Dazza051197
4 Comments
 
LVL 4

Expert Comment

by:jerch
ID: 6207255
You can use System.getProperty("user.dir") to get the current directory where your Java program is running on.

Jerson
0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 100 total points
ID: 6207647
Hi,

Why don't you put your text file into jar and use Class.getResourceAsStream()?
If you still want to find your directory, here is a piece of code from http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11475783:

private String classResName(){
    String className = getClass().getName();
    return "/"+className.replace('.','/')+".class";
}

private File findExtRoot(){
    URL url = getClass().getResource(classResName());
    try{
       URLConnection connection = url.openConnection();
       if( connection instanceof JarURLConnection ){
       URL jarURL = ((JarURLConnection)connection).getJarFileURL();
           File jarFile = new File(jarURL.getFile());
           return new File(jarFile.getParent());
       }
   }
   catch(IOException err){
       err.printStackTrace();
   }
   return null;
}

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6209162
Yes Dazza

Use getResourceAsStream("FileName");


Shyam
0
 
LVL 1

Author Comment

by:Dazza051197
ID: 6209584
Thanks Igor

With a little bit of tweaking to the code you gave me, it now works.  The code you gave me works fine for when the code is running from the Jar file, but if I'm still in Forte it returns null, so I managed to add some code to the else of the findExtRoot() to return the filepath if it's not running from the Jar file.

Thanks
Dazza
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now