0

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!

3
Contributors
11
Replies
12
Views
8 Years
Discussion Span
Last Post by moutanna
-1

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"));

0

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..?

0

It must work event if you ommit the implicit variable "this"
What message you get please?
or if you can post the whol code

0

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:

public class test2 {
  public static void main(String args[]) {
     String className= getClass.getName();
     ClassLoader cl=Thread.currentThread().getContextClassLoader();
     System.out.println(cl.getResource(className + ".class"));
  }
}
0

Add "import java.io.File;" then use this code:

File f = new File("./");
        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.

0

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 ();
}
}

0

still gives me an error at getClass.getName();
but thank you anyways :)

0

Did you try my idea bellow?

import java.io.File then use this code:

File f = new File("./");
        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).

0

if you tray excecuting the code inside the main method directly you will have an error because you coud not reference a non static method getClass from a static context main.
But using the test method as decribed a bove it works.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.