user2197116
user2197116

Reputation: 677

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.

Upvotes: 18

Views: 65471

Answers (5)

Phil A
Phil A

Reputation: 1

If you don't want the properties file to be copied to the bin folder, you can try the following:

  1. Right click your project, select Build Path, select Configure Build Path..

  2. Select Libraries tab

  3. Select Add class folder..

  4. Add your resource folder.

Upvotes: 0

Rajesh Guna
Rajesh Guna

Reputation: 21

Follow these steps to get this issue fixed:

  1. Right click on Project
  2. Click on Run As and select Run Configurations
  3. Click on the classpath tab (Oxygen Eclipse)
  4. Click on user entries
  5. Click on Add External JARs.. and choose the downloaded JAR file
  6. Click Apply and run your project...

Upvotes: 2

user3324410
user3324410

Reputation: 71

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

Upvotes: 7

Vivek Vardhan
Vivek Vardhan

Reputation: 41

Right Click on the project-name in Package Explorer, select Properties, select Java Build Path on the left, select Source tab on the right, click on Add Folder, browse through the project's directories to select the resources folder or whatever you need to add to the eclipse classpath, hit OK, again hit OK. Done.

Upvotes: 1

E-Riz
E-Riz

Reputation: 32895

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.

Upvotes: 27

Related Questions