Solved

absolute path of current class file ?

Posted on 2001-09-05
18
395 Views
Last Modified: 2009-07-29
hi,
    can any one tell me how to retreive the absolute path of any running class file. i can get the path of folder from where i am running my application but i need the path of folder where my class file is residing.

i tried,
System.getProperty("user.dir")
and
File f = new File ("");
f.getAbsolutePath() ;

both gives only the path from where i am running my application not the path where these codes is being executed.

 thanks in advance for any help.



 
0
Comment
Question by:shahnazali
[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
  • 7
  • 6
  • 2
  • +2
18 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6456241
Your class may not be running from a class file on disk.
It may be contained in a jar file, or loaded over the network.
0
 
LVL 6

Expert Comment

by:kotan
ID: 6456259
try this,

public class path {
    public static void main(String[] args) {
        System.out.println(System.getProperty("user.dir") +
                                System.getProperty("file.separator") +
                                    new path().getClass().getPackage().getName());
    }
}
0
 
LVL 1

Author Comment

by:shahnazali
ID: 6456295
file is neither in jar nor in network.......it's in same machine like normal file.

 actually i am using package say
package A.B.C ;

and my file path which i need dynamically is
C:\Temp\A\B\C\XYZ.class

and i set my class path to "C:\Temp" so that i can execute my class file from any  folder.

now if i run my file from any folder, these codes gives currrent folder path only.. not what i want.......
like if i run this from
D:\Shaan folder the output will be D:\shaan not C:\Temp\A\B\C

check it out.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 1

Author Comment

by:shahnazali
ID: 6456310
file is neither in jar nor in network.......it's in same machine like normal file.

 actually i am using package say
package A.B.C ;

and my file path which i need dynamically is
C:\Temp\A\B\C\XYZ.class

and i set my class path to "C:\Temp" so that i can execute my class file from any  folder.

now if i run my file from any folder, these codes gives currrent folder path only.. not what i want.......
like if i run this from
D:\Shaan folder the output will be D:\shaan not C:\Temp\A\B\C

check it out.
0
 
LVL 1

Author Comment

by:shahnazali
ID: 6456331
file is neither in jar nor in network.......it's in same machine like normal file.

 actually i am using package say
package A.B.C ;

and my file path which i need dynamically is
C:\Temp\A\B\C\XYZ.class

and i set my class path to "C:\Temp" so that i can execute my class file from any  folder.

now if i run my file from any folder, these codes gives currrent folder path only.. not what i want.......
like if i run this from
D:\Shaan folder the output will be D:\shaan not C:\Temp\A\B\C

check it out.
0
 
LVL 92

Expert Comment

by:objects
ID: 6456363
Try:

URL location =
  getClass().getResource("/A/B/C/XYZ.class");
0
 
LVL 1

Author Comment

by:shahnazali
ID: 6456400
file is neither in jar nor in network.......it's in same machine like normal file.

 actually i am using package say
package A.B.C ;

and my file path which i need dynamically is
C:\Temp\A\B\C\XYZ.class

and i set my class path to "C:\Temp" so that i can execute my class file from any  folder.

now if i run my file from any folder, these codes gives currrent folder path only.. not what i want.......
like if i run this from
D:\Shaan folder the output will be D:\shaan not C:\Temp\A\B\C

check it out.
0
 
LVL 6

Accepted Solution

by:
kotan earned 50 total points
ID: 6456405
Try,

ClassLoader.getSystemResource("A/B/C/XYZ.class").getPath()
0
 

Expert Comment

by:hanumansetti
ID: 6456432
U can use "System.getProperties" or "System.getProperty methods" in java.lang

Mainly

user.home - User's home directory
user.dir  - Working directory
java.class.path - Java class path
java.ext.dirs   - Path of extension directory or directories

0
 
LVL 92

Expert Comment

by:objects
ID: 6456433
Copycat :)
0
 

Expert Comment

by:hanumansetti
ID: 6456456
U can use "System.getProperties" or "System.getProperty methods" in java.lang

Mainly

user.home - User's home directory
user.dir  - Working directory
java.class.path - Java class path
java.ext.dirs   - Path of extension directory or directories

0
 
LVL 1

Author Comment

by:shahnazali
ID: 6457579

heartly thanks for all responses.....

 but now can any body tell me why i am getting this exception on using getPath() method over any URL object.

System.out.println (ClassLoader.getSystemClassLoader().getSystemResource("A/B/C/CurrentDirectory.class").getPath());

Exception in thread "main" java.lang.NoSuchMethodError: java.net.URL: method get
Path()Ljava/lang/String; not found
        at A.B.C.CurrentDirectory.main(CurrentDirectory.java:42)
-------------------

 even if i create url like

URL url = new URL ("http://www.yahoo.com/shaan/index.html");
String s = url.getPath() ;

gives me same error like above ...... y ?

0
 
LVL 92

Expert Comment

by:objects
ID: 6459008
getPath() doesn't exist in 1.1 (which is why I didn't mention it :)).

You can try using getFile() instead.

0
 
LVL 1

Author Comment

by:shahnazali
ID: 6473431
thank u object..... but i am using 1.3

 then ..... where is the problem.
0
 
LVL 92

Expert Comment

by:objects
ID: 6473465
Well that's strange.
From the 1.3 javadoc:

public String getPath()
Returns the path part of this URL.
Returns:
the path part of this URL.

If your compiling with 1.3 then I don't understand why it cannot find the getPath() method.


0
 
LVL 1

Author Comment

by:shahnazali
ID: 6473808
now i got the problem. It's a run time error.

 My system has both 1.2 as well as 1.3 java run time environment. so while compiling it will be taking from 1.3 and during running it will be taking from 1.2 . and getPath()  method is not there in 1.2 . this simple problem blow up my mind.

 thanks again man for ur contribution.

bye.
Shaan
0
 
LVL 92

Expert Comment

by:objects
ID: 6475461
Yes if you want to run the class with a lower cersion of the JVM then you cannot use the method.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6961464
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.11632018.html
http://www.experts-exchange.com/questions/Q.11931118.html
http://www.experts-exchange.com/questions/Q.20008478.html
http://www.experts-exchange.com/questions/Q.20178672.html
http://www.experts-exchange.com/questions/Q.20283239.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.11687658.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations if this item.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Chrome and Firefox Java 5 88
spring maven example issues 3 97
What is the use of Forwarding Class in java 1 51
DTD and JAVA versions 1 55
In this post we will learn different types of Android Layout and some basics of an Android App.
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 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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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