views:

795

answers:

9

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  A: 

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?

Myles
This is the filename:public static final String PROP_FILENAME="src/conf.properties";
craftsman
Under Java '/' works well even on Windows systems, actually Visual c++ handles '/' as well.
ypnos
Agreed, however '\' works well under windows too, not so much in linux.
Myles
A: 

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

Woot4Moo
If you put a \ in a path for Windows, you'll need to \\ it since the \ is the escape character. If you use the / then you don't need to escape it.
Jay R.
thanks for the extra information, I was speaking off of a JBoss context and my trials and tribulations using it :)
Woot4Moo
+2  A: 

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

dustmachine
+1  A: 

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

ypnos
+2  A: 

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.

Andrzej Doyle
+4  A: 

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.

Jay R.
+1: the problem is that he's using relative paths and thus relying on the current working directory.
BalusC
+2  A: 

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

JuanZe
+1  A: 

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

anish
+4  A: 

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.

Björn
Thanks for your response Bjorn and everybody.I just unpacked the JAR file with WinRAR, and found that there was no src/ directory at all there and all the content of src/ was in root directory. I had no idea that JAR changes the structure like this.I simply used the method Bjorn mentioned and set the path as "/conf.properties" and things started working.Thanks again.
craftsman
When packaging with jar, your directory structure should not change. Maybe you have some setting in your IDE that move the files?
Björn
I am using Eclipse and I never touched any such settings. It is a standalone application and I exported it as a Runnable JAR. However I'll find out how Eclipse offers such settings.
craftsman