File paths in Java (Linux)

2020-07-17 14:52发布

问题:

I have created a Java application that loads some configurations from a file conf.properties which is placed in src/ folder.

When I run this application on Windows, it works perfectly. However when I try to run it on Linux, it throws this error:

java.io.FileNotFoundException: src/conf.properties (No such file or directory)

回答1:

If you've packaged your application to a jar file, which in turn contains the properties file, you should use the method below. This is the standard way when distributing Java-programs.

URL pUrl = this.getClass().getResource("/path/in/jar/to/file.properties");

Properties p = new Properties();
p.load(pUrl.openStream());

The / in the path points to the root directory in the jar file.



回答2:

Instead of

String PROP_FILENAME="src/conf.properties";

use

String PROP_FILENAME="src" + File.separator + "conf.properties";

Check the API for more detail: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html



回答3:

I would also check what your current working directory is if your path to that file is relative. You just need to make a File test = new File("."); and then print that files canonical path name.

If you are referencing any other locations like user.dir or something to that effect by using System.getProperty(), you'll want to at least verify that the directory you are using as the relative root is where you think it is.

Also, as Myles noted, check the slashes used as file path separators. Although you can always use the "/" and it works.

And if you are referencing the path absolutely, you'll have trouble going between one OS and another if you do something silly like hard-code the locations.



回答4:

What you want to do is check out System.getProperties() and look for file.separator. The static File.pathSeprator will also get you there.

This will allow you to build a path that is native for whatever system you're running on.

(If indeed that is the problem. Sometimes I like to get the current directory just to make sure the directory I think I'm running in is the directory I'm really running in.)



回答5:

Check your permissions. If you (or rather, the user that the Java process is running under) doesn't have appropriate permissions to read the file, for example, you would get this error message.

This is a typical Windows -> Linux migration problem. What does ls -l src/conf.properties show when run from a prompt?

Additionally, check capitalisation. Windows isn't case-sensitive, so if the file was actually called e.g. CONF.properties it would still be found, whereas the two would be considered different files on Linux.



回答6:

You should check the working directory of your application. Perhaps it is not the one you assume and that's why 'src' directory is not present.

An easy check for this is to try the absolute path (only for debugging!).



回答7:

I would check your slashes, windows often uses '\' vs linux's '/' for file paths.

EDIT: Since your path looks fine, maybe file permissions or executing path of the app is different?



回答8:

check your slashes and colons in my case i set my PS1 to following value

PS1='\n[\e[1;32m]$SYSNAME(\u)@[\e[1;33m]\w [\e[1;36m](\d \T) [!]\e[0m]\n\$ '

i am trying to read from the env .such as system.getenv

Java was throwing exception

java.lang.IllegalArgumentException: Malformed \uxxxx encoding



回答9:

Try the double slash, after doing things in JBoss I often had to refactor my code to use the double slashes