Find Relative Java Application Path

I read all the other questions related to this on StackOverflow, and I did not find a clear answer. To shorten it, I have an application that will store some files in a directory that I will use than to process them. I have intentions to move my application to different places (other computers), so I need to have a relative path to work so that I don't change this every time. Does anyone know how to get the relative path of the application (and not the full path) so that I can use in this case? If what I ask is not talking, tell me another way to achieve what I need. thank you

+4
source share
2 answers

Just use "./" .

Regardless of the directory in which your application was run, "./" will always return this directory.

For instance:

new File("./") will return the file object specified in the directory from which your Java application was launched from

new File("./myDirectory") will return the file object specified in the myDirectory folder located in the directory from which the Java application was launched from

+3
source

Here is one approach:

I believe that you need to determine the path of the directory containing the files in the configuration / property file. You can change the path in the configuration file when moving the application or directory containing the file. This content of your properties file (say config.properties) should be:

filesDirPath = \ USR \ main \ test

And this is what you should do in the code:

  private void readConfig() { Properties prop = new Properties(); try { //load a properties file prop.load(new FileInputStream("config.properties")); //get the directory path property value String flesDirPath = prop.getProperty("filesDirPath"); System.out.println("Files to be read are located in dir : " + flesDirPath ); } catch (IOException ex) { ex.printStackTrace(); } } 
+1
source

Source: https://habr.com/ru/post/1485423/


All Articles