0

When I run the jar on cmd it do not search the .properties file and hence the complete functionality is not established. My .properties file is under the /src folder. How can I resolve this issue?

try {
    // Loads the constants.properties file
        InputStream constantPropFile = 
            ClassLoader.class.getResourceAsStream
 ("/account.properties");
        constantProp.load(constantPropFile);
    }  catch (IOException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }
PS.
  • 13
  • 6
  • 4
    And how is your class path set? See parameter -cp or -classpath. Could you describe where is your class stored and where is your properties file stored? – JiriS Jun 04 '15 at 08:24
  • What is the specific error you got? – mazhar islam Jun 04 '15 at 08:33
  • how do i need to check the classpath? – PS. Jun 04 '15 at 08:44
  • You said your properties file is located in the `/src` folder. Is your output folder during compile time the same as the `/src` folder? If you have a different output folder for the classes did you copy the properties file to the `classes` (output folder of the compile process) folder? Or did you expicitly add your properties file to the jar? You can check of the jar contains your properties file by `jar t your.jar`. Another option is to add your `/src` (the directory where the properties file is located) by `java -cp -jar your.jar ` – andih Jun 04 '15 at 08:59
  • You may refer [this][1] question. It looks similar. [1]: http://stackoverflow.com/questions/19424308/add-properties-file-to-build-path-of-runnable-jar – AmolB Jun 04 '15 at 09:04
  • D:\Postcard_workspace_new\Postcard_workspace\ConfigMigrationUtility\bin;D:\Postcard_workspace_new\Postcard_workspace\ConfigMigrationUtility\lib\org.apache.commons.io.jar This is my classpath and my properties file are in D:\Postcard_workspace_new\Postcard_workspace\ConfigMigrationUtility\src – PS. Jun 04 '15 at 09:05
  • My output folder has different location as that of /src folder. Also i have not explicitly added the properties file to my jar. Also I tried your -cp command given above it throwed an exception as "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at configmigrator.utility.ConfigMigrator.main(ConfigMigrator.java:15)" – PS. Jun 04 '15 at 09:12
  • you can watch that:http://stackoverflow.com/questions/8775303/read-properties-file-outside-jar-file – Eduardo Pérez Jun 04 '15 at 09:17
  • @Perez I have already gone through it. It do not give my solution – PS. Jun 04 '15 at 09:26
  • I think, the purpose of a properties file is to be able to edit it, so I think that must be the solution, if you get the file inside the jar, there is no diference between your properties and a constants java class. – Eduardo Pérez Jun 04 '15 at 11:06
  • My jar is working well on local systems. But when I run it on Virtual Machine I dont get required output – PS. Jun 04 '15 at 11:18

0 Answers0