4

I'm new to java , i have file named config.porperties i put it in this path /home/user/workspace/myproject/config.properties but when i submitted the jar file after packaged it i got that it cannot find this file

java.io.FileNotFoundException: config.properties (No such file or directory)

the code

FileInputStream finputstream = new FileInputStream(
                "/home/user/workspace/myproject/config.properties");
prop.load(finputstream);

but there is no error in the code i think the problem with jar file that it couldn't read the path !

Hope i can find help , THANKS

  • Possible duplicate of [FileNotFoundException when file exists with all permissions](http://stackoverflow.com/questions/2312924/filenotfoundexception-when-file-exists-with-all-permissions) – Bacteria Dec 16 '15 at 07:44
  • 2
    @UUIIUI No, that is not a duplicate. Here the problem is that we cannot load _resources_ from a JAR as doing it with regular _files_. – Seelenvirtuose Dec 16 '15 at 07:47
  • yes that's right so i edited the question title with jar file –  Dec 16 '15 at 07:47
  • Can you open jar with a unzip program and check config.properties at there? – hurricane Dec 16 '15 at 07:47
  • Look at my answer for [Cannot load properties file from resources directory](http://stackoverflow.com/questions/20348657/cannot-load-properties-file-from-resources-directory) for how to deal with resources in a JAR file. – Seelenvirtuose Dec 16 '15 at 07:48
  • 1
    "/home/user/workspace/myproject/config.properties" is a hard coded absolute path. Jar possibly has myproject/config.properties. You need to use relative path if jar doesn't show this path for file. – Sabir Khan Dec 16 '15 at 07:49
  • @hurricane i unzipped it already and found the file –  Dec 16 '15 at 07:50
  • @Sabir_Khan please what is the relative path like what ? –  Dec 16 '15 at 07:54
  • If the error only states 'java.io.FileNotFoundException: config.properties (No such file or directory)' that means that for some reason in your code you are not using the whole path, but just ' input = new FileInputStream("config.properties");'. Is it possible you are using an old jar, or in your code you have another call to this? – jolumg Dec 16 '15 at 07:55
  • @joseluismg (and all others): The problem is the code itself. You cannot _load a file_ from inside a JAR file with the regular File API. You have to use a [`ClassLoader`](https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html) and [get a resource](https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResource-java.lang.String-) from it – Seelenvirtuose Dec 16 '15 at 07:58
  • 1
    @Begnnier : if you are using an IDE ( Eclipse etc ), create a folder "config" in your project, put your file there and include that folder as included resource in project settings. Change code to - FileInputStream finputstream = new FileInputStream("config/config.properties"); and that should work. Relative means path relative to your project directory. – Sabir Khan Dec 16 '15 at 07:58
  • @Sabir_Khan thanks i'll try it now –  Dec 16 '15 at 07:59
  • i got this error [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project myproject : There are test failures. –  Dec 16 '15 at 08:04
  • @Begnnier, just for clarification, is your config.properties included in your jar? or is it a simple external file? – jolumg Dec 16 '15 at 08:16
  • @joseluismg the file in my jar –  Dec 16 '15 at 08:51
  • in that case you have do what @Seelenvirtuose is suggesting you: you can not use directly the path because the properties file is inside of the jar. You have to do something like InputStream input = getClass().getResourceAsStream("/classpath/to/my/file/config.properties"); – jolumg Dec 16 '15 at 09:10

1 Answers1

0

I am not sure what happend during "but when i submitted the jar file after packaged it". Your code still tried to load home/user/workspace/myproject/config.properties. So check whether that file exists and is readable.

If you want the file to reside on a different path - you have to change your code as the path is hardcoded.

if you expect the config.properties to be packaged within the jar and loaded from there, change your code to something like

InputStream input = getClass().getResourceAsStream("/classpath/to/my/file/config.properties");

Watch out: Again you are hardcoding a path but you can ensure the file will reside at that location within the jar.

Queeg
  • 7,748
  • 1
  • 16
  • 42