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(...){...}
Related
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)"
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'm developing a Dynamic Web Project in Eclipse. I created a .properties file for store database details (Username, Password etc.). I added it by right clicking on the project and New -> File . I used the Java util package Properties class. But it does not working. I can not retrieve any property from the file. Here is the code I used,
Properties prop = new Properties();
try {
prop.load(new FileInputStream("database.properties"));
String db = prop.getProperty("database");
String userName = prop.getProperty("dbuser");
String password = prop.getProperty("dbpassword");
} catch (IOException ex) {
ex.printStackTrace();
}
Is there something wrong or Is there any particular place where I should put properties file.
What you did is correct, ie right clicking the project and new--file.You have to Put your properties where you start your jvm from. Please look into the attached image. The properties file is marked in red. Look if your properties file is also located something like this.
Also add this in your code to find out where to put your file:
System.out.println(new File(".").getAbsolutePath());
For more details please follow this link- FileNotFoundException when using java properties file
Normally, you make sure the properties file is in the project runtime classpath (e.g. WEB-INF/classes) and then load it using either the System classloader or the property file handler's classloader, i.e. (Freehand typing from memory -- NOT COMPILED)
try{
Properties p = new Properties();
InputStream in = MyPropertyHandler.getClass()
.getClassLoader()
.getResourceAsStream("com/package/props/database.properties");
p.load(in);
catch(IOException e){
e.printStackTrace(System.err);
}
I'm betting you aren't pointing at the correct location. Make sure you're properties file is in the correct place. Using that code, I believe it is looking for ${CURRENT_WORKING_DIR}/database.properties, which is the case of a web app in eclipse is WEB-INF/classes (i think).
You should instead be using the more portable java.util.Properties#load(InputStream) with the result of javax.servlet.ServletContext#getResourceAsStream(String).
Try to give absolute path or relative path to the proprty file, also check this propery file path has been add to source folders or not, if not it will not be copied to your classes folder. (Right cclick on project , check java build path under source tab.
You should have .properties file in same package as class that is using it.
Or better, read properties file with getResourceAsStream method (otherwise you can have some problem later when you'll have file in .war archive).
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("database.properties");
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().