Solved

Returning path to class file

Posted on 2001-06-19
4
163 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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes Dazza

Use getResourceAsStream("FileName");


Shyam
0
 
LVL 1

Author Comment

by:Dazza051197
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

763 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

9 Experts available now in Live!

Get 1:1 Help Now