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
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)"
Related
I work on a Java console application. There is a property in my application.properties file, which contains another file name as a value of a property, like
my.file.location=file:myDir/myFileName
In the code I try to get the file like this:
#Value("${my.file.location}")
private File myfileLocation;
If I start the application from the directory, which contains jar file, the file is resolved, but when I run my application from a different location, the file location is not valid.
I can't have this file on classpath, it must be external to the jar file.
How can I make the file path to be relative to my jar file and not to the current working directory?
I believe this has nothing to do with Spring right? You just want to load configuration file, that is inside your application, unpacked, so the user can modify it, ok?
First, you may try to always setup the working directory, which I believe is more "standard" solution. In windows you can make a link, that specifies the Start in section and contains the path to your jar file (or bat or cmd, whatever).
If you insist on using the jar path, you could use How to get the path of a running JAR file solution. Note, that the jar must be loaded from filesystem:
URI path = MySpringBean.class.getProtectionDomain().getCodeSource().getLocation().toURI();
File myfileLocation = new File(new File(path).getParent(), "/myDir/jdbc.properties");
i'm doing a program in java, that will be exported in a runnable JAR and executed in windows as service using YAJSW, i have to read a config.ini file that have important params for the execution, but i'm setting a fixed path:
Path configFile = Paths.get("D:\\Folder\\config.ini");
The problem is that i don't know the path where it will be executed on final user pc.
i tried this:
Path txtParametro = Paths.get("\\config.ini");
because the .ini file will be in the same folder of .jar, but didn't work.
Someone has any idea of how i can handle this?
I thought of environment variables ... but would have to manually do it, is not an option.
You can set the file to be created in a specific location so that the program will always know where it will be:
File file = new File("D:\\Folder\\config.ini");
if (!file.exists()){
file.createNewFile();
}
Regards,
Thomas
If the file is part of the JAR then you could load it like this:
Class.getResourceAsStream("config.ini");
As described here:
How do I access a config file inside the jar?
If not in A JAR please let us know.
Found a solution, that way:
assuming that config.ini is in the same folder of .jar
File pathJAR = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());
it returns me the path of running jar, "D:\Folder\name.jar", then i replaces "name.jar" by "config.ini".
not so beatiful, but works.
How to load property files placed in resource folder of an executable jar file. Here my app itself is a jar and it executes on it own. It need to find this property file (placed within itself under resource folder) at runtime depending on the path mentioned in the code. I have used below two methods but it didn't help me. Point here is, both these options are working fine when i execute in eclipse, but doesn't work when I pack it into an executable jar. It throws NullPointerException. Only problem I see here is that jar is not able to pick the property files with given path. Any help would be appreciated.
Method 1: Using Apache Commons Configuration
URL propFileURL = XYZ.class.getClassLoader().getResource("/config.properties");
Configuration propertyConfiguration = null;
propertyConfiguration = new PropertiesConfiguration(propFileURL);
In above case I'm getting ConfigurationException. Class is not able to find file mentioned in given path.
Method 2: Using getResourceAsStream. I know that getResource doesn't work if we are to load files from network on in any other location.
InputStream is =XYZ.class.getClassLoader().getResourceAsStream("/config.properties");
Properties prop = new Properties();
prop.load(is);
In this case, I'm getting nullPointerException.
Let me know if you need more details.
jar content Heirarchy
Properties file - /java-file-io-application/src/main/resources/config.properties
XYZ class - /java-file-io-application/src/main/java/org/bc/xyz/iplugin/utilities/XYZ.java
Looks like you might be building your jar incorrectly. Files from 'src/main/resources' would be expected at the root of the jar file. If your jar file contains the 'src/main/resources' directory, something's off with your build.
I have the problem running executable .jar file. I've created a project which contains a .properties file. It works just fine when I start it from eclipse, but when I export it to executable .jar file and try to run it with:
java -jar myfile.jar
I get the following exception:
(couldn't post image here)
http://imageshack.us/photo/my-images/824/29583616.png/
I've checked my manifest file in the .jar and it contains the
Class-Path: .
And here's the properties file loading:
properties = new Properties();
properties.load(new FileInputStream(
"src/com/resources/treeView.properties"));
Any idea what causes this exception?
If the properties file is inside the jar file, you cannot access it as a file.
You need to ask the classloader to get the resource as an inputstream. See Getting the inputstream from a classpath resource (XML file)
In Eclipse (and in most IDEs) the current directory is the project's root directory. This means that Class-Path: . means something else in Eclipse than when you run it from the command line. This is why you wrote "src/com/...". Remove "src":
properties.load(new FileInputStream("com/resources/treeView.properties"));
Your properties file is within JAR file. So, use : ClassLoader.getResourceAsStream().
propsdatabase = new Properties();
InputStream dbin = getClass().getResourceAsStream("/properties/database.properties");
propsdatabase.load(dbin);
I am reading my database connection details via a properties file which is named 'database.properties' in a folder named 'properties'. Jus below the root directory.
The code was perfectly working fine when the jar was exported in Eclipse.
But I used Maven project in IntelliJ to get the jar . It throws NUll pointer exception .
Since the Value of dbin is NULL.(I printed and checked also).
I conclude that the path is not recognised to read the file.
Now The things are fine with IntelliJ .
While doing an export as jar in Eclipse the jar although contains propertioes folder IT is not detected. pl help
The reason that getResourceAsStream is returning null is that the /properties/database.properties file is not in the Maven classpath.
Move your properties folder to under /src/main/resources folder and when Maven creates a jar file, the /properties/database.properties resource will be included, and you'll stop getting the NPE.
Yes, getResourceAsStream is no doubt returning null. I suspect that your jar file doesn't include the file. Try running:
jar tvf yourfile.jar
to check. Next, check the build steps for the jar file. It's hard to say what's wrong without seeing the build file, so please edit your question to include that information if it doesn't leap out at you when you look at the steps.
Does your maven build step include the properties file in the jar? Check the jar-file that is produced. If you don't know how you can always rename it and add a ".zip" at the end and open it.
You may try to read the properties file using the path and a FileInputStream:
Properties properties = new Properties();
FileInputStream input = null;
try {
input = new FileInputStream(new File(CONFIGURATION_FILE));
properties.load(input);
}catch(...){...}