English 中文(简体)
File paths in Java (Linux)
原标题:

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)
最佳回答

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.

问题回答

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

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.

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

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.

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

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?

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

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

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

Java was throwing exception

java.lang.IllegalArgumentException: Malformed uxxxx encoding

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签