Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Returning path to class file

Posted on 2001-06-19
4
Medium Priority
?
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

704 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