Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 454
craftsman Avatar asked Nov 30 '22 06:11

craftsman


2 Answers

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.

like image 188
Björn Avatar answered Dec 04 '22 21:12

Björn


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

like image 27
JuanZe Avatar answered Dec 04 '22 21:12

JuanZe