Adding libraries to your java project
A question I see once in a while in #eclipse is How do I add libraries to my java or plugin project? or How do I fix errors like ‘The import org.eclipse.foo cannot be resolved’?
The latter is particularly common when you’re working with someone else’s code and they didn’t provide you with a complete project (including up to date .classpath), or when you’re updating a project that worked on an old version of Eclipse to a newer one, and a library you used to use has been removed, renamed, or updated.
There are two approaches:
a) Hacker approach: open up your project’s .classpath file and add the libraries you need, such as:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src"
path="src"/>
<classpathentry kind="output"
path="bin"/>
<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="lib"
path="/path/to/eclipse/plugins/org.eclipse.xsd_2.3.0.v200706262000.jar"/>
<classpathentry kind="var"
path="ECLIPSE_HOME/plugins/org.eclipse.osgi_3.3.0.v20070530.jar"/>
<classpathentry kind="var"
path="ECLIPSE_HOME/plugins/org.apache.ant_1.7.0.v200706080842/lib/ant.jar"/>
<classpathentry kind="var"
path="ECLIPSE_HOME/plugins/org.apache.ant_1.7.0.v200706080842/lib/ant-launcher.jar"/>
</classpath>
where ECLIPSE_HOME, a classpath variable, will resolve to wherever you’re running Eclipse, eg., ~/eclipse or c:\eclipse.
b) Graphical approach:
- Right-click your project and select ‘Properties’ (or select it and hit ALT-ENTER).
- On the left side of the dialog, select ‘Java Build Path’, then select the ‘Libraries’ tab at the top
- Click ‘Add Variable…’, select ECLIPSE_HOME, then click ‘Extend…’
- Browse for the library you want to add, the hit ‘OK’
Of course, you can add things other that extended variables. You can add workspace jars, non-workspace (external) jars, or entire libraries (like JUnit or JSF). Then, rebuild your project (or just open the individual .java files) and your unresolved classes and imports should be gone.
For more, see Adding an extra library to a project.
Posted September 15th, 2007 by in category: ant, build, classpath, faq, jdt
You can skip to the end and leave a response. Pinging is currently not allowed.
3 Responses to “Adding libraries to your java project”
Leave a Reply
You must be logged in using your Eclipse Bugzilla account to post a comment.



Wassim Melhem Says:
September 15th, 2007 at 04:55 pm
Nick,
What you are suggesting is fine for Java projects, but it is an example of what NOT to do for plug-in projects. A plug-in project’s classpath must never be tweaked manually to add external JARs.
If a plug-in needs to add Ant classes to its classpath, it must declare a dependency on the “org.apache.ant” plug-in on the Dependencies page of the plug-in manifest editor. Once you save the manifest file, PDE automatically updates the plug-in classpath to include the Ant JARs.
If you manually tweak the .classpath file of a plug-in project, you will compile at development time, however you will get classloading exceptions at runtime.
Eugene Kuleshov Says:
September 15th, 2007 at 09:32 pm
I always wondered why JDT and PDE didn’t provide a quickfix for adding theserequired jars/plugins. It was quite easy to implement such feature for m2eclipse’s classpath container and I don’t see any technical difficulties to do the same for other containers.
http://docs.codehaus.org/display/M2ECLIPSE/Dependency+Quick+Fix
Wassim, as far I can remember it is still necessary to manually kick update project classpath after tweaking plugin’s manifest using plugin.xml/manifest editor and it is really strange that PDE has this weird duplication 3 palces (manifest, build.properties and .classpath)
Nick Boldt Says:
September 16th, 2007 at 10:05 pm
Wassim: good point.
Eugene: also a good point. Open a feature request!