I was trying to work on a java program with requires a properties file attached to the jar. Thus, i did jar the program, and unjar it to attach a properties file. I have a class, MyClass, which requires the properties file. Thus, i basically put the properties file to the same folder as MyClass.class. Then I jarred it back.
In my MyClass.java, i was trying to access the properties by:
Properties prop = new Properties();
prop.load(MyClass.class.getClassLoader().getResourceAsStream("prop.properties"));
Where prop.properties is in the same folder as my MyClass.class.
It failed to load it. Any help will be appreciated !
Thanks,
ClassLoader.getResourceAsStream() treats the path as relative to the root of the classpath, i.e. it looks for resources at the root level of each JAR or directory on the class loader.
You probably want Class.getResourceAsStream, which treats the path as being relative to the package of the class in question. You also need to make sure to close the stream once you've read it.
Properties prop = new Properties();
InputStream in = MyClass.class.getResourceAsStream("prop.properties");
try {
prop.load(in);
} finally {
in.close();
}
If MyClass is in the package com.example then this will load com/example/prop.properties from the JAR.
Related
I am creating an application (hive udf) and for that I am creating an fat jar with all the dependencies. As this is going to be distributed over multiple nodes I need to pack my property files in jar as well.
Problem
One of the function I am calling from another jar needs a parameter, a property file path. I have packed this property file inside jar. and passing just the name. However, it's complaining that file not found.
Code:
c = new ABC("classification.props");
Function inside ABC class that read the property file
public static Properties getProperties(String propFile) {
Properties props = new Properties();
try {
props.load(new FileInputStream(propFile));
return props;
} catch (IOException var3) {
var3.printStackTrace();
}
}
This ABC is coming from another jar. however I am creating a fat jar so shouldn't matter. classification.props is in the root folder of application.
The getProperties method is looking for a file on the filesystem. That's not where your file is -- your file is in the JAR.
If you can change the getProperties` method, you can tell it to read the file as a class path resource instead.
How do I load a file from resource folder?
If you can't modify the getProperties method, you can copy the properties to the filesystem (load it as a class path resource using the technique in the linked answer, and then write to to a temp directory).
I've been able to get the following to work by loading a properties file which exists in the same folder as the class files (the bin folder)
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("config.properties");
I want to load the config.properties file from a resources folder which is at the same level as the bin folder. If I pass it "resources/config.properties" it finds the file in /bin/resources, but passing it "/resources/config.properties does not look above the bin folder like I want.
Ideas?
Try to use Properties like this:
Properties props = new Properties();
InputStream resourceStream = Your_Class_Name.class.getResourceAsStream("config.properties"))
props.load(resourceStream);
class.getResourceAsStream method loads from jars and folders defined in your classpath. If you have that config.properties file under resources folder, you should add that folder in your classpath.
Something like
java -classpath "resources" ...
I have the following project structure:
ProjectName/src/java/com/main/Main.java
And I have a properties file in the following folder:
ProjectName/config/settings.properties
Now I tried to load the properties file in the Main.java with:
InputStream input = Main.class.getResourceAsStream("/config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
But this does not work. How is it the right way to do it?
Edit: I got the following error:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
Edit2: Now it works with eclipse. I did the following (adapted from getResourceAsStream() is returning null. Properties file is not loading)
1) This directory [config] must be put into the "build path". Right-click the directory in the Package Explorer or Project Explorer view, select "Build Path", then "Use as Source Folder". Note: This build path will be the class path for the project, when you run it.
2) As the config directory now is part of your class path and contains your properties file, you can simply load it with InputStream input = Server.class.getClassLoader().getResourceAsStream("settings.properties");
This works well for eclipse but not yet for a jar. I think I also have to add the build path somehow to the ant script.
How can I do that?
Edit3: If I take the settings.properties out of the config folder in the jar, then it works. Why? I want it in the config folder in the jar too.
Use following :
InputStream input = Main.class.getResourceAsStream("../../../../config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
By adding ../ I am getting 1 step backward in your filesystem. In your code you were using /config....., which means C:/config..... (if it is in C drive).
The following will also work in your case. (but not in zipped file)
InputStream input = new FileInputStream("config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);
getResourceAsStream find files relatively compiled (class) files on in jar file. Example:
Your compiled files are in bin directory than you should put properties file to bin/config/settings.properties for give access to it.
You are using maven. So follow the maven structure and keep your resources like prop, xml etc files in resource folder and then call.
So folder structure would be like this
src/main/resource|
|
config|
|
settings.properties
InputStream input = Main.class.getClass().getResourceAsStream("/config/settings.properties");
Three things
You need add the settings.properties to a jar (can be the same jar as your Main)
Your classpath should include this jar
You need to use the context Class Loader to load the properties file in you Main.
Classloader cl = Thread.currentThread().getContextClassLoader();
cl.getResourceAsStream("settings.properties");
I have a properties file named xyz.properties that is in the root folder of my Java Project. Now I want to change the location to some other place (say Desktop on Windows).
Is the properties file have to be on the root folder? Could my Java application read it from another location outside the project?
Any help is appreciated.
If properties file is on the classpath then it can be loaded using the generic code as mentioned here:
Properties props = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
I have created a dynamic web project within Eclipse. I created a properties file inside the src directory:
<project-root>/src/props.properties
I'm starting Tomcat via Eclipse in debug mode. Now I want to read the properties file from one of my POJOs, but I get a FileNotFoundException. The current path seems to be the Eclipse path.
I had a look into the web for solution, but none of them worked for me. Maybe I did something wrong. The code is like this:
File file = new File("props.properties");
FileReader reader = new FileReader(file);
properties.load(reader);
How should I acces the properties file?
Where should it be located?
Thanks in advance.
If its a web application then the properties will get deployed to WEB-INF/classes and can be loaded using the class loader
InputStream in = {NameOfClassWhereThisisInvoked}.class.getResourceAsStream("/props.properties");
properties.load(in);
in.close();
Actully that should work regardless of whether it is a webapp or not.
I'm assuming src is defined as a source folder
There is another way of doing this as follows:
File file = new File(session.getServletContext().getRealPath("/") + "props.properties");
Instead of "props.properties" you could give any path relative to the "WebContent" folder of your eclipse web-application project.
here is the correct way to load properties file from anywhere in the classpath
private Properties getPropertiesFromClasspath(String propFileName)
throws IOException
{
Properties props = new Properties();
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream == null)
{
throw new FileNotFoundException("property file '" + propFileName
+ "' not found in the classpath");
}
props.load(inputStream);
return props;
}
You can create a singleton class to load properties in memory on first time access and later use them via a static method. A complete example of this is available at
http://bharatonjava.wordpress.com/2012/09/12/using-properties-file-in-java-application/
Kepp ur Properties file in src folder and the following code is enough to read the properties file
**File file = new File("./src/config.properties");
FileReader reader = new FileReader(file);
prop.load(reader);
System.out.println(prop.getProperty("directorypath"));**
Is your project set to build automatically? If so (or you're building manually), check whetherprops.properties appears in your project's output folder (in Eclipse this is usually "bin" but may be WebRoot or something else, depending on how your project is set up).
When a project builds, it should copy over config files as well as your compiled classes, JSPs etc. However, I believe that file readers, by default, use the JVM's home directory for their source. In Eclipse's case, this means that your props.properties file would have to be in the project root, i.e. not the "src" folder.