Determinng currently running java file location?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 17
Reputation: darangho is an unknown quantity at this point 
Solved Threads: 0
darangho darangho is offline Offline
Newbie Poster

Determinng currently running java file location?

 
0
  #1
Oct 16th, 2009
Hi all,
I am trying to write a code that determines
the path of the currently running java file.

I know that in python, you do

os.path.join(sys.path[0], sys.argv[0])

in order to figure this out,

but is there any equivalent code like this in java?


Thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: moutanna is an unknown quantity at this point 
Solved Threads: 6
moutanna moutanna is offline Offline
Light Poster
 
-1
  #2
Oct 16th, 2009
You can use the ClassLoader to do that:

String className=this.getClass.getName();//Name of your class
ClassLoader cl=this.getClass.getClassLoader();
System.out.println(cl.getRessource(className + ".class"));

Or you can use it with the Thread Object:

cl= Thread.currentThread().getContextClassLoader();
System.out.println(cl.getRessource(className + ".class"));
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 17
Reputation: darangho is an unknown quantity at this point 
Solved Threads: 0
darangho darangho is offline Offline
Newbie Poster
 
0
  #3
Oct 16th, 2009
Thanks for reply!

It gives me an error at this.getClass.getName();
Saying getClass cannot be resolved or is not a field.

seems like the error arose
because, the java file doesn't have a constructor,
there is no object being created before the call getClass.

Do you have any idea to solve this..?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: moutanna is an unknown quantity at this point 
Solved Threads: 6
moutanna moutanna is offline Offline
Light Poster
 
0
  #4
Oct 16th, 2009
It must work event if you ommit the implicit variable "this"
What message you get please?
or if you can post the whol code
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 17
Reputation: darangho is an unknown quantity at this point 
Solved Threads: 0
darangho darangho is offline Offline
Newbie Poster
 
0
  #5
Oct 16th, 2009
I removed this. but still has an error.
It's saying:

1 error found:
File: C:\Documents and Settings\dolee\Desktop\test2.java [line: 3]
Error: C:\Documents and Settings\dolee\Desktop\test2.java:3: cannot find symbol
symbol : variable getClass
location: class test2

and I am testing in a test class file,
the code is like:

  1. public class test2 {
  2. public static void main(String args[]) {
  3. String className= getClass.getName();
  4. ClassLoader cl=Thread.currentThread().getContextClassLoader();
  5. System.out.println(cl.getResource(className + ".class"));
  6. }
  7. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training
 
0
  #6
Oct 16th, 2009
Add "import java.io.File;" then use this code:
  1. File f = new File("./");
  2. System.out.println(f.getAbsolutePath());
This will tell you the absolute path of the folder in which the java app is running (the "*.jar" file).
Hope I helped.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: moutanna is an unknown quantity at this point 
Solved Threads: 6
moutanna moutanna is offline Offline
Light Poster
 
0
  #7
Oct 16th, 2009
try this please:
public class test2 {
public void test(){
String className= getClass.getName();
ClassLoader cl=Thread.currentThread().getContextClassLoader();
System.out.println(cl.getResource(className + ".class"));

}
public static void main(String args[]) {
new test2 ();
}
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 17
Reputation: darangho is an unknown quantity at this point 
Solved Threads: 0
darangho darangho is offline Offline
Newbie Poster
 
0
  #8
Oct 16th, 2009
Yeap this gives me a path to the file
Thank you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 17
Reputation: darangho is an unknown quantity at this point 
Solved Threads: 0
darangho darangho is offline Offline
Newbie Poster
 
0
  #9
Oct 16th, 2009
still gives me an error at getClass.getName();
but thank you anyways
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training
 
0
  #10
Oct 16th, 2009
Did you try my idea bellow?

import java.io.File then use this code:
  1. File f = new File("./");
  2. System.out.println(f.getAbsolutePath());
This will tell you the absolute path of the folder in which the java app is running (the "*.jar" file).
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC