2

When i compile and run my program in Eclipse, is runs without exceptions. But when i export it to an runnable jar or an normal jar it does not find my settings file.

My path is:

String path= "src/settings/settings.ini";

In eclipse it runs without exception, but in a jar, it throws the exception immediately .

How is it possible to make the jar just work as it is in eclipse?

Damodaran
  • 10,882
  • 10
  • 60
  • 81
Pim_D
  • 89
  • 1
  • 10
  • 1
    Check this question : http://stackoverflow.com/questions/12090882/trouble-turning-java-project-into-jar-file – phsym Sep 11 '12 at 11:24
  • 1
    *"it throws the exception"* What exception? Please always copy/paste exception output (the stacktrace). – Andrew Thompson Sep 11 '12 at 11:36

2 Answers2

5

while inside jar the file is no more physical file, so you may need to read it as Stream

getClassLoader().getResourceAsStream("settings/settings.ini");

considering that settings.ini is under classpath (runtime) at specified path

jmj
  • 237,923
  • 42
  • 401
  • 438
  • I find `getResource(String)` for an URL is not only more reliable for getting access to an [embedded resource](http://stackoverflow.com/tags/embedded-resource/info) (in that the `InputStream` returned is typically not repositionable), but also shorter to type. ;) – Andrew Thompson Sep 11 '12 at 11:34
0

If the ini file is located in the jar archive, read it like this :

inputstream = getClass()
          .getClassLoader()
          .getResourceAsStream("settings/settings.ini");

Else if it's located outside of your jar archive, you should move or copy your ini file in ./settings/settings.ini (relatively to the current working directory of your application).

phsym
  • 1,364
  • 10
  • 20