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!

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

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

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

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

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.

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

Yeap this gives me a path to the file
Thank you!

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

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

Yeap the one with File object works fine.
Thank you! :D

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.