1

I have a java .jar file I created using NetBeans. I am using apaches procrun (prunsrv.exe) to install that .jar as a Windows Service. I modified the code to get a property from a config.properties file. I added the config.properties file to the same folder that my .jar file resides in. My code is as follows:

Properties props = new Properties();
InputStream inputStream = MyService.class.getClassLoader().getResourceAsStream("config.properties");
props.load(inputStream);

On the last line of my code, I am getting a NPE when I attempt to start my service. I assume this is because the file is not found. I modified the manifest.mf as follows:

Class-Path: .

I also tried copying config.properties to the "lib" folder (subfolder to where my .jar file is located). Same results.

I modified the "set PR_CLASSPATH" line in the batch file that installs the service as follows:

set PR_CLASSPATH=MyService.jar;.

Still same NPE.

How can I get my code to recognize my config.properties file once the service has been installed?

Thanks, Raymond

1 Answers1

1

This is what I use to load resources in these situations and seems to work most of the time:

public static InputStream getResourceAsStream(String path) {
    return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}

Could you check, if this helps in your case?

Another strategy to overcome this problem is the one described in my comment:

"copy the configuration file to a specific absolute folder path (i.e. c:\test) and change the classpath to point to that folder (set PR_CLASSPATH=MyService.jar;.;c:\test)"

gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
  • Same issue. I passed "config.properties" to that method and the InputStream is still null. – Raymond Kelly Oct 04 '17 at 16:02
  • Can you copy the copy the configuration file to a specific absolute folder path (i.e. c:\test) and change the classpath to point to that folder (set PR_CLASSPATH=MyService.jar;.;c:\test). And then retry? – gil.fernandes Oct 04 '17 at 16:07
  • That did it. After I tried that and it worked, I simply created a "resources" folder under my root and added that to my CLASSPATH. I believe my problem was the usage of the manifest.mf in conjuction with the "set PR_CLASSPATH" in my install.bat. I removed Class Path entry in my manifest.mf and modified the PR_CLASSPATH to include all I needed. – Raymond Kelly Oct 04 '17 at 16:26
  • So your problem seems to be solved now :) I have updated the question to include the comment which helped, so that you can mark the question as solved. – gil.fernandes Oct 04 '17 at 16:32