Tastybrownies Tastybrownies - 1 year ago 60
Java Question

Source Folder in Runnable Jar Disappearing

I've tried working on this for a couple hours and still couldn't get it right so I figured I would ask. I am trying to use log4j to write program statements to an output file and have this working on my local machine just fine.

I currently have my log4j.properties file in a folder called logging. It is marked as a source folder. When generating my runnable JAR file via eclipse, I place it on another server to run and it says the log4j.properties file cannot be found. I've tried a few different things so far and nothing is working. All I want to do is make sure the log4j.properties file is included in the runnable JAR. My setup is the following right now.

Project foler - called test


In my Java program I have the following code that is working locally. Am I missing some fundamental step during the JAR generation process in Eclipse? Seems like this should be easy but its not right now. I've even extracted the JAR in 7zip after creation and sure enough, the logging file is not there at all. Its like it has complete disappeared for some reason. I would greatly appreciate it if anyone could help me out, thank you.

public static void main(String[] args)

FileInputStream fis = new FileInputStream("logging\\log4j.properties");

if(fis != null)

logger.info("process started.");


Instead of using InputStream directly, I modified my code to just use the below and it is now working, accessing the log4j.properties file through the runnable jar.

logger.info("Conversion process started.");

Answer Source

I see 2 issues: a) when you create a jar using eclipse, by default it only zips the compiled classes (so .class files) into your target .jar file, you need to specify to eclipse that you also want to include the resources of your project, these can be config files like a properties file for log4j, but can also be resource bundles for i18n messages etc.... An explanation how to do this can be found here: Java & Eclipse: Resources in JAR?. So if the properties for log4j are not in the jar file then double check the jar file creation!

b) if you have the resources in your jar file, you cannot read them using a fileinputstream: they are not stored in a standalone file, they are stored in the jar file. I assume that your jar file is part of the classpath, so when you have achieved adding the resources into your jar file, you need to treat them as resources: see Load resource from anywhere in classpath for more info on this subject