Solved

Returning path to class file

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example issue 2 25
Java: anonymous class 4 38
swing controls 2 21
Where to place postgres JDBC driver jar on tomcat 8 26
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…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

756 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