Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a directory to the eclipse classpath?

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.

like image 983
user2197116 Avatar asked Aug 06 '14 14:08

user2197116


People also ask

How do I add a class to my classpath?

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.


2 Answers

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.

enter image description here

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.

like image 145
E-Riz Avatar answered Sep 22 '22 16:09

E-Riz


This is for a maven project:

  1. Right click on project
  2. click on run configurations
  3. click on the classpath tab (Oxygen Eclipse)
  4. click on user entries
  5. click on Advanced
  6. first radio selection default should be 'Add Folders'
  7. click OK
like image 32
user3324410 Avatar answered Sep 20 '22 16:09

user3324410