Link to home
Start Free TrialLog in
Avatar of shahnazali
shahnazali

asked on

absolute path of current class file ?

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.



 
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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.
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());
    }
}
Avatar of shahnazali
shahnazali

ASKER

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.
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.
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.
Try:

URL location =
  getClass().getResource("/A/B/C/XYZ.class");
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.
ASKER CERTIFIED SOLUTION
Avatar of kotan
kotan
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Copycat :)
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


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 ?

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

You can try using getFile() instead.

thank u object..... but i am using 1.3

 then ..... where is the problem.
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.


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
Yes if you want to run the class with a lower cersion of the JVM then you cannot use the method.
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.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://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.
https://www.experts-exchange.com/questions/Q.11632018.html
https://www.experts-exchange.com/questions/Q.11931118.html
https://www.experts-exchange.com/questions/Q.20008478.html
https://www.experts-exchange.com/questions/Q.20178672.html
https://www.experts-exchange.com/questions/Q.20283239.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://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 https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> https://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