I'm attempting to run an already existing eclipse project created by another person.
After importing it to eclipse, and attempting to Run As->Java Application, it fails because it cannot find a .properties file in bin/resources
I printed out the classpath eclipse was using
logger.info(System.getProperty("java.class.path"));
and sure enough, it includes bin, and all the lib/*.jars, but not bin/resources. Copying the .properties file into bin makes the program work, but I wanted to understand how to add a directory to the eclipse classpath.
I tried several things, none of which worked
export CLASSPATH=$CLASSPATH:/home/me/programdir/bin/resources
This didn't work. I understand it's not a desirable way to approach the issue, but I had thought it would fix the problem (I have more of a windows than linux background so perhaps I am missing some nuances of system variables in linux)
Next up, I tried modifying the VM arguments in the Run->Configurations dialog in Eclipse
-classpath "/home/me/programdir/bin/resources"
No luck here either, which confused me, as I was sure it would work and seemed like a reasonable solution to a specific program needing one additional folder added to the classpath.
Next I tried modifying build.xml directly. I found the part that defines the classpath and added my own line for bin/resources, as follows:
<path id="classpath">
<fileset dir="./bin/resources" includes="**/*.properties"/>
<fileset dir="./lib" includes="**/*.jar" />
</path>
this too was unsuccessful. This perplexed me even more, so I commented out the entire path element, and the classpath printed out by the logger was unchanged, so it is apparent that whatever classpath eclipse was using, it certainly wasn't this one. This seemed to me the best solution, had it worked: the build.xml file could be checked in with the correct additions to prevent future users from experiencing the problem.
Next I tried the IDE approach. Run->Configurations->Classpath-> User Entries->Advanced and simply added the bin/resources folder. That worked perfectly, the program finds the properties file, all is well. However, I am dissatisfied that my previous efforts failed without me really understanding why. It seems that each one should have worked.
Additionally, I want to ensure that I fix this problem in such a way that it is captured by the code I check in so that subsequent users do not have to go through the same steps. My solution is thus not very satisfactory as I am not sure what actual piece of code changed, and thus cannot verify that the 'fix' is checked in.
How do you find the actual definition that eclipse is using for its classpath? I had thought it would be the build.xml classpath definition, but that did not seem to be the case at all.
This can also be set in the Eclipse GUI by right-clicking on the project, selecting Properties->Java Build Path. Then select Add External Class Folder and enter the directory where you want to store your classes.
In Eclipse, there is a build classpath and a runtime classpath. There is also the build output location, which by default is bin
. You don't want to add resources directly to bin
because Eclipse can delete its contents when doing a clean build. What you need to do is add a resources
folder in your project to contain any non-Java files that you want included in your build output.
To include the contents of this resources
folder in the build output (bin
), right-click the project and select Properties. In the Project Properties, select the Java Build Path section, then the Source tab.
Use the Add Folder... button to select the resources
folder from your project, then OK to save the changes. At that point, Eclipse will automatically copy everything from resources
into bin
when it builds.
This is for a maven project:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With