javaprocrun

prunsrv service with external properties file


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


Solution

  • 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)"