1

I tried most of the answer on Stack Overflow and they give path where my .java file is not present. How to get path where my *.java file is actually present or exist?

Suppose name of my java file is Test.java and it's location is src/main/java/mypackage/Test.java . So I want to print path src/main/java/mypackage.

I deploy my files to some server and then run it there, whever I want to print the path, I get locations like jar:file/ where no files are present.

  • Can you please elaborate what you want to achieve? Why do you need the path to your Java file? If your program is compiled to a JAR and deployed there might be no Java file at all on the system. So your question as is (without context) doesn't make any sense. – vanje Jun 29 '21 at 09:10
  • @vanje I want to make list of files present in directory and sub-directory by traversing through them. So for that I need to know current directory location –  Jun 29 '21 at 09:15
  • You probably want to read [this question](https://stackoverflow.com/questions/4871051/how-to-get-the-current-working-directory-in-java) (in particular its second most-upvoted answer which is a more modern solution) but you'll find out your current directory generally isn't the one that contains your .java file – Aaron Jun 29 '21 at 09:21
  • `System.getProperty("user.dir")` gives you the initial directory of your application. When called from the IDE you will find the java files there or in subdirectories. –  Jun 29 '21 at 09:22
  • I'd like to clarify, while I gave a possible answer to the question, I don't think it's a good idea. What are you ultimately trying to achieve? Why do you want to print the path of the files? – Seth Falco Jun 29 '21 at 09:23
  • @k314159 I believe @QuinncyJones is correct. `user.dir` provides the user working directory, not the home directory. See: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html – Seth Falco Jun 29 '21 at 09:27
  • @SethFalco except the current directory is the one the java process was launched from (in an IDE, generally the project's root I believe, but in other contexts it can be anywhere) and won't directly contain the .java file – Aaron Jun 29 '21 at 09:28
  • System.getProperty("user.dir") is provided in lot of answers and it never worked –  Jun 29 '21 at 09:29
  • 2
    You deploy class files, not java source files. Generally the source files are only on your development machine. – NomadMaker Jun 30 '21 at 02:09

2 Answers2

3

You cannot get path of current Java file by itself, because Java program executed by Java binary byte code in *.class file(s). Your question is meaningless.

Simple logic, when you put *.class file to another directory, what happen with path result? Specific path will change while *.java file is still in its location.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
2

It's important to note that the location may vary depending on if you're running it in your IDE, or running it from a .jar file.

In development, you can do this using the ClassLoader.

import java.net.URL;

public class Main {

  public static void main(String[] args) {
    ClassLoader loader = Main.class.getClassLoader();
    URL url = loader.getResource("Main.class");
    System.out.println(url);
  }
}

Output when executing from code editor:

file:/tmp/vscodesws_92b4a/jdt_ws/jdt.ls-java-project/bin/Main.class

Output when executing jar file:

jar:file:/home/seth/Downloads/build/Main.jar!/Main.class

I don't think this is a good idea. Especially after you compile your project to a jar, war, or whatever else.


It's important to look at Do Nhu Vy's answer as well. Notice how my solution provides the path to .class files, not .java files.

Seth Falco
  • 313
  • 6
  • 22