-3

I am having trouble finding a way to have Java link the path of the directory where the project files are located.

I am aware of this and other versions that does the same thing

System.getProperty("user.dir"))

But this only links to the working directory and I really need it to link to where the files are. This way program will always read the file I need it to read without there ever being any issues with not the correct path.

I know how to do this in Python but I cannot find a way to do it in Java

In python it is easy, is this lacking in Java is that why I cannot find anything that does it?

os.path.dirname(__file__) 

Do I need to implement one line of code in python to make this work? Seems kinda odd there must be a way to make this work like in Python.

public class Test {

   public static void main(String argvs[]) {
        String file = "/directoryname/filename";
        System.out.println(System.getProperty("user.dir")+ file); //I want this to link to the directory the Class is located so it can open up a directory there. 
    }
}
Jon doe
  • 13
  • 3
  • Java is designed to work in a "networked" environment, so the path to the files might be a link over the internet. I'm not 100% sure that what you want exists. – markspace Jul 20 '23 at 15:57
  • @markspace Then I have spent hours on a goose chase I had a feeling did not exist so I guess I add a 1 line python code file to the project then. – Jon doe Jul 20 '23 at 16:03
  • 2
    Its not clear what you are after as the "directory where the project files are located" and "finding the running java class path" aren't the same thing. – DuncG Jul 20 '23 at 16:03
  • This might get you something useful, but be careful that Java generally has a more complex execution environment than many other languages: https://stackoverflow.com/questions/227486/find-where-java-class-is-loaded-from – markspace Jul 20 '23 at 16:09
  • @DuncG then my apologies I have done a misstake asking the question then. essentially I want the file paths location where the projects java files are located. No matter where you execute the code from. – Jon doe Jul 20 '23 at 16:17
  • @markspace I have tried this before and I am now looking more at the path for the .java files but I think I can maybe modifty this to make it work I assume it is possible to make java take a step backwards out from the bin files – Jon doe Jul 20 '23 at 16:23
  • 1
    @Jondoe The compiler doesn't record the path to original source file, and class file might be in directory or a jar. Perhaps you should add background on what you are trying to achieve as there are likely to be other ways to handle your problem. – DuncG Jul 20 '23 at 16:30
  • *This way program will always read the file I need it to read* Which file is that? If it's a resource (read-only data used by your app) they will be packaged with your app and always available, provided that's done correctly and they are addressed correctly. You've probably got an 'XY problem' here – g00se Jul 20 '23 at 16:46
  • So what you want is to see where the current class is inside the jar / classPath? – Calabacin Jul 20 '23 at 16:47
  • @@DuncG lets say I have the Java project named Test and the path goes like this c:/projects/Test/src/test/Test.java using the Java inbuilt methods I can only access the project file path like c/projects/Test but I want it to go all the way for example if you were to run the code in the c/projects directory in visual studio code instead it would not read the files if you use the in built methods like get.property("user.dir") + "/images/image.png" this only works if you run the file from the ''correct'' directory. I do admit it is a minor problem I am greatly overthinking. – Jon doe Jul 20 '23 at 16:52
  • @DuncG essentially I am trying to make sure it executes properly even if you run the project wrong. – Jon doe Jul 20 '23 at 16:54
  • *. I do admit it is a minor problem I am greatly overthinking.* You are correct to acknowledge it as it is an important consideration. Where you are incorrect is that you are not looking at the correct way of solving it. There are two assured ways of addressing files that are needed by the app: a. as *resources* (read-only) and b: as files in absolute paths that are reliably writable-to [e.g. `$user.home`] (read-write) – g00se Jul 20 '23 at 16:55
  • @g00se could you explain more? I did not quite follow that. – Jon doe Jul 20 '23 at 17:01
  • First of all you need to be a lot clearer on what files you're concerned about and what you're doing with them – g00se Jul 20 '23 at 17:05
  • @g00se I am just uploading a sprite of something drawn using graphics. I ecountered an issue of running the code when I had a different working directory with the path. As mentioned. The issue is minor as to have the issue appear you would need to be outside of the Java programs intended working directory. I am trying to link the correct path even if you were to execute it outside the directory. – Jon doe Jul 20 '23 at 17:14
  • example eclise you can only execute the code from the working directory but visual studio code allows you too more or less execute it from anywhere and that rruins things if you do. – Jon doe Jul 20 '23 at 17:16
  • You need to treat your sprite file [as a resource](https://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse) – g00se Jul 20 '23 at 17:27
  • @g00se I will look into this and hope it will solve the issue thank you. – Jon doe Jul 20 '23 at 17:35
  • @g00se many thanks I seem to have gotten it working now :) – Jon doe Jul 20 '23 at 18:14
  • Good. So you're using `getResourceAsStream`? – g00se Jul 20 '23 at 19:09
  • @g00se yes, is that an issue? I might looking into alternatives. – Jon doe Jul 21 '23 at 04:56
  • No. That's what you should be using – g00se Jul 21 '23 at 07:34
  • @g00se I see, then I understood it. Again many thanks, now the code executes in any working directory I can find, I also now have noticed it is much easier to set up Java projects in eclipse then vs code – Jon doe Jul 21 '23 at 10:39

1 Answers1

-1

Have you tried the following?

String path = new File(".").getAbsolutePath();

I have created this program:

public class Main {
    private String getPath() {
        return new File(".").getAbsolutePath();
    }

    private void showPath() {
        System.out.printf("Full path is %s", getPath());
    }

    public static void main(String[] args) {
        new Main().showPath();
    }
}

Copied the created jar to another folder and run it using this command:

java -jar showPath-1.0.jar

And that gave me the exact path where I was running it from.

Calabacin
  • 725
  • 2
  • 8
  • 19
  • I have yes, only gives me directory part sorta. It would not work if you ran the code outside of the specific workspace path. LIke running the code and you run it outside the implemented work space path it will crash the code. – Jon doe Jul 20 '23 at 16:15
  • @Calabacin. You solved a different problem. That of not knowing the absolute path of the current directory. – g00se Jul 20 '23 at 16:51
  • I still do not understand what you want. I suggest you improve your question. – Calabacin Jul 20 '23 at 16:54
  • @Calabacin Apparently what I am looking for is not possible really in Java from my understanding of the issue. Because of the way Java executes. Well sorta the code can me modified to help with the issue of actually executing it wrong. – Jon doe Jul 20 '23 at 17:04
  • I'm pretty sure it can be done, I am just unable to know what it is you want. You could for example get the package from the current class, which is its path inside the JAR, if that is what you want. In any case I do not understand this need, as you have a resource folder that can contain whatever you need, and you may also read the proper folder from a config file in the current path, receive it as a parameter, or even read an environment variable. – Calabacin Jul 20 '23 at 17:08
  • @Calabacin I forgot to reply, it was simply me overthinking the issue and not fully understanding the problem. And of course me not understanding the issue I could not give a complete description what the problem was. Thank you for your effort still. – Jon doe Jul 20 '23 at 18:45