Solved

Returning path to class file

Posted on 2001-06-19
4
168 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 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

739 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