Access property file from within JAR Maven project - java

After reading about 50 different threads on the subject, I have not been able to have any of the solutions work.
What I'm trying to do is to access my config.properties file within the jar.
My project is a Maven project and the architecture is as follows:
My project
src/main/java
myApp
MyClass.java
src/main/resources
icon.jpg
config.properties
So when I try to access it from Eclipse it works perfectly good with this code:
InputStream configFile = MyClass.class.getResourceAsStream("config.properties");
Followed by more code here to retrieve properties...(that works)
However, when I export the project to a runnable JAR, it doesn't work any more. Inside the jar, files are as follows:
File.jar
MyApp
files.class here
etc...
META-INF
resources
icon.jpg
config.properties

Try this:
InputStream configFile = MyClass.class.getResourceAsStream("resources/config.properties");

Try the following:
InputStream input = AnyClass.class.getResourceAsStream("/config.properties");
It should work.

Related

resources folder is not getting found in eclipse with InputStream in = getClass().getResourceAsStream("resources/template.properties");

I have an awt application which will be run using executable JAR file. Few Issues I am facing in getting resources folder
1. In Eclipse, If I use following code
properties = new Properties();
InputStream in =getClass().getResourceAsStream("resources/template.properties");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Then I am getting
ERROR 2019-11-11 12:03:44,052 [AWT-EventQueue-0]
java.lang.NullPointerException
If I use the above code in eclipse and when I export runnable Jar then it is working in JAR file but when I run in eclipse it is throwing nullpointer exception.
Case 2: But if I use below code and export JAR file then JAR file is throwing error as "in current directory resources is not available" even resources folder is present.
properties = new Properties();
InputStream in =
inputStream = new FileInputStream("resources/template.properties");
Folder structure
enter image description here
You shouldn't reference the resource folder in the file path string. But then, your resource folder needs to be a source folder for Eclipse.
I don't know your project strcuture yet, but I suggest you to create a resource folder on your project root, then rigth click on it and go to Build Path > Use As a Source Folder.
After that, all files inside that folder will both be directly accessed by the getResourceAsStream method, and also be placed on the JAR root, merged with all other files from the other source folders.
Then you can do:
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("template.properties");
This way should work both inside Eclipse and in the standalone JAR.
PS: I strongly suggest you to start using Maven projects and follow its folder structure.
You don't need to specify the resources path if you do like this snippet bellow it might work.
InputStream inputStream = getClass()
.getClassLoader().getResourceAsStream("template.properties");

Gradle and src/main/resources

I have a gradle project which is working fine within eclipse. In the java code I have
Properties p = MyClass.class.getClassLoader.getResourceAsStream("myProps.properties");
File f = new File(p.getProperty("myFile"));
My structure is:
src/main/java - has java files
src/main/resources - has myProps.properties and myFile.txt
myProps.properties has:
myFile=src/main/resources/myFile.txt
If I do a gradle build (using a default shadowJar task) my resources have now been moved from src/main/resources to the root of the jar file.
shadowJar {
manifest{
// The only thing I modify is the attributes
}
}
When I execute the jar by running
"java -jar myJar.jar" I get FileNotFound exception referring to the myFile.txt resource file.
What is the correct way to fix this issue so that the code works from both the Eclipse and executable jar environments?
I needed to read the file as a stream (the same way I read in the properties file). I removed 'src/main/resources' from the path in the properties file and it worked

using directories from java jar application

I have java app, where I copy directories (with subdir and files) to SDCard. I wanted to save these dirs with project. In eclipse everything works fine. But, when I export project to runnable jar, app is not able to find or copy these dirs. No error is showed.
File folder2 = new File(getClass().getResource("/urgent/").getFile());
File folder4 = new File(getClass().getResource("/service/").getFile());
The path, I get from jar app, when I want to copy dir is:
jar:file:\C:\path\to\jar\file\myapp.jar!\urgent
project hierarchy:
-project
--src
--build
--urgent
---subdir
--service
---subdir
Is it possible to work with your own directories in jar and how?
You are trying to work on resources on you classpath, a random folder on your harddrive is not in it, so you cannot find it.
What you need to do is:
1. Work with parameters from the commandline (see how to use args[] in your main method)
2. use those arguments to parametrize your custom copy mehod
or:
Try using the copyDirectory(File srcDir, File destDir) method from the Apache Commons IO library instead. To use this reearch how to include libraries in your project, preferably by using maven.

how to make a runnable jar for a java project with properties files will be editable at runtime?

My maven project is a standalone java appplication. I need to run this form a Unix box. So i made a runnable jar.But i have to update a date in the app.properties file in every run.I tried maven jar plugin to make a fatty runnable jar with all dependency. It is running fine , but not able to edit the app.properties file
You can read a properties file from the classpath (root package) like this:
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("/app.properties")));
You can start a runnable jar with a custom classpath like this:
java -cp app.properties:app.jar my.main.AppClass
(you cannot use java -jar because then the -cp option is ignored)
Put the file app.properties in the same directory as app.jar. The location of app.properties will be the first entry on the classpath and thus the code in the jar can load it as shown above.
If you make sure there is also a copy of app.properties in the jar, then that will be used as default if the external properties file is missing. (note that this only works for the complete properties file, not for individual properties)

Java: How to package and access resources inside a runnable Jar file?

I have a Java console applicaton with a resources folder. It works OK, but when I want to export my code to a runnable Jar file(with Eclipse), then the exported Jar can not find the files(they are in buildpath) and give a Filenotfound exception.
When I unzip the exported Jar file I can see the files are there in the root folder, so I guess something wrong with my code.
BufferedReader srcFile = new BufferedReader(new FileReader("resources/"+filename));
String line = srcFile.readLine();
I tried to use
URL url= ClassLoader.getSystemResource(filename);
filename=url.tostring();
But no luck.
Use getResourceAsStream(String) (docs) if you want to read in a resource on the classpath.

Categories

Resources