Solved

absolute path of current class file ?

Posted on 2001-09-05
18
390 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

895 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

14 Experts available now in Live!

Get 1:1 Help Now