How to add external library properly in Eclipse? - eclipse

So today I downloaded Apache Commons Lang library (binary, zip format). I extracted it to C:\eclipse\commons-lang-2.5 folder. There are a commons-lang-2.5.jar, a commons-lang-2.5-javadoc.jar, and a commons-lang-2.5-sources.jar inside, and a folder for HTML Javadoc. I started Eclipse, added commons-lang-2.5.jar, and set its source and Javadoc respectively as the screenshot below. (http://img43.imageshack.us/img43/9378/eclipsev.png)
My question is, is there a convenient or standard way to add external libraries? Or am I actually doing the right thing?

Recommendation:
Create a "lib" folder and keep all your jars in the folder.
Subsequently, add all the jar files in the lib folder into your build path by using Project => Properties => Java Build Path => Libraries => Add JAR ...
btw, there' no screenshot. Can you give the link for the screen shot so that I may be able to help our better...

You must add jar file on lib folder
and then right click on jar file and click "build path"-->add to build path
and now you can write that jar file code

Use maven
You don't have to download all jar's into a folder by yourself! -
use maven. It's based on a public repository, and you manage your dependencies in an xml file.
Your project will have a pom.xml file that going to look like this:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
and in this file you manage the external library dependencies
for instance, if you wish to add this dependency - http://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4
you will need to modify your pom.xml like so:
<project>
...
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
...
</project>
and save it. that's it
Read this getting started tutorial - https://maven.apache.org/guides/getting-started/
eclipse plugin - https://maven.apache.org/plugins/maven-eclipse-plugin

As the question is quite general, maybe following detailed answer could help others:
Right click on your project name
Select and expand the correct (e.g C/C++) compiler
Go to "Includes"
On right hand corner, you would see a small icon to "add path" to include your custom library
Add, build and happy compiling

Related

Mapping maven structures to Eclipse structures, then analyzing with JDT

I'm trying to generate Java unit tests using Eclipse's JDT. Part of that is determining the classpath that pertains to a particular Java class.
I imported a Maven project named jaffa-framework into Eclipse. Jaffa-framework has several Maven modules, including jaffa-api. Each of the submodules has its own pom.xml. I can build jaffa-framework and its submodules with no problem using Maven from within Eclipse (although it will not build using Project -> Build Project).
<project ...
<artifactId>jaffa-framework</artifactId>
...
<profiles>
<profile>
<id>normal</id>
<modules>
<module>jaffa-api</module>
...
</modules>
</profile>
</profiles>
...
</project>
When I import jaffa-framework into Eclipse as a Maven project, I see the following in the UI:
A Java project: jaffa-framework
Two Folders/IPackageFragmentRoots for jaffa-api:
jaffa-api/src/main/java and jaffa-api/src/test/java
One "plain" folder: jaffa-api
This structure looks weird to me. I expected to see something more like:
jaffa-framework
jaffa-api
jaffa-api/src/main/java
jaffa-api/src/test/java
Perhaps I did something wrong while importing? If you have ideas, please let me know.
If I select a Java file within jaffa-api/src/main/java, I can use JDT to navigate to the jaffa-framework project and get its classpath using javaProject.getResolvedClasspath(true); however, what I really need is the classpath for jaffa-api.
The jaffa-api folder contains a pom file, a .project file, and a .classpath file. I don't see an easy way to navigate to it using JDT, and because it is a folder, I don't see how I could obtain the classpath programmatically using JDT.
I'm also wondering why jaffa-api looks like a plain folder, instead of something more project-like. Eclipse must know that it is some kind of subproject, because the folder contains .classpath and .project files.
Please tell me how I can access the classpath information of jaffa-api using JDT, and/or how I can correct my project set up.
There was something screwy about my original project creation. I'm not sure what.
I solved my problem by deleting my jaffa-framework project by right-clicking on it and choosing "Delete" from the menu. I re-imported the project using File -> Open Projects from File System. I'm guessing the key thing to notice here is that the "Search for nested projects" box is checked. After the project was reloaded, both jaffa-framework and jaffa-api appeared as projects.

How to set Maven dependencies on a local jar file

I am starting to play around with Maven, to see whether we could use it in the future to handle our dependency management, and IDE environments.
I have looked at some YouTube vids on how to get started with Eclipse (we also use Eclipse), and where you basically start off with creating a new project of type Maven. I have done this, and imported my existing source into the src/main package type.
Now I want to start adding the dependencies. No changes to my pom file yet.
I have two directories with jar files in them, and I need to set those dependencies in the pom file.
How do I do that?
This is not how you usually use Maven. You can add a jar through a path
<dependency>
<groupId>org.javap.web</groupId>
<artifactId>testRunWrapper</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/testRunWrapper.jar</systemPath>
</dependency>
but the recommended way is to draw your jars from a Maven repository (like MavenCentral, or your Nexus/Artifactory).
So if you want to use Maven in your company, make sure you have a running Nexus or Artifactory server in your company as well. Then you can either proxy external Maven repositories (which contain most of the available open source components) or upload your own jars through the interface of your Nexus/Artifactory.

Debug a local maven dependency with eclipse

i have two maven projects, the first one is a library and the other one use it to works properly, the both have to elvolve regardless each other, this is why i use two different project.
But breakpoints on my library code doesn't work when i launch my app (the second application).
This is how i include my library in the second project's POM (my IDE is eclipse and projects are in the same workspaces)
<dependency>
<groupId>com.mcamier</groupId>
<artifactId>lazyEngine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/../lazyEngine/target/lazyEngine-0.0.1-SNAPSHOT-jar-with-dependencies.jar</systemPath>
</dependency>
I think the quick fix to this is to right-click on your application project in Eclipse and choose Properties, remove the library jar from the external jars, and add the library Eclipse project to projects (i.e. the list of project dependencies).

In Maven: how to attach sources to tools.jar

I have to use libraries in tools.jar and have therefor added this dependency:
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
Now I would like to attach the sources in Eclipse, so I see what I develop against.
I guess nothing will be available in the standard maven repositories (I can't even find anything in download.java.net/com/sun).
I know there is a source archive available, but it contains everything and I would at least need to know what parts of it need to go in the source jar.
Has someone done this? Is there a tools-sources.jar available somewhere? Or can you tell me what parts of the jdk sources I need?
Here's what I did. I downloaded the source archive from openjdk, extracted it and manually linked the jar source to
External Folder -> [unpacked archive basedir]/langtools/src/share/classes
This is not a maven solution, it's eclipse only, but it works.

Get source jar files attached to Eclipse for Maven-managed dependencies

I am using Maven (and the Maven Eclipse Integration) to manage the dependencies for my Java projects in Eclipse. The automatic download feature for JAR files from the Maven repositories is a real time saver. Unfortunately, it does not include API documentation and source code.
How can I set up Maven to automatically also get the source and javadoc attachments and register them properly with Eclipse?
I am sure m2eclipse Maven plugin for Eclipse - the other way around - can do that. You can configure it to download both the source files and javadoc automatically for you.
This is achieved by going into Window > Preferences > Maven and checking the "Download Artifact Sources" and "Download Artifact JavaDoc" options.
mvn eclipse:eclipse -DdownloadSources=true
or
mvn eclipse:eclipse -DdownloadJavadocs=true
or you can add both flags, as Spencer K points out.
Additionally, the =true portion is not required, so you can use
mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs
The other answers on this work, but if you want to avoid having to remember command line arguments, you can also just add to the downloadSources and downloadJavadocs config to the maven-eclipse-plugin section of your pom.xml:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plugins>
</build>
...
</project>
I prefer not to put source/Javadoc download settings into the project pom.xml file as I feel these are user preferences, not project properties. Instead, I place them in a profile in my settings.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>sources-and-javadocs</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sources-and-javadocs</activeProfile>
</activeProfiles>
</settings>
Right click on project -> maven -> download sources
If the source jars are in the local repository and you are using Eclipses maven support the sources are getting automatically attached. You can run mvn dependency:sources to download all source jars for a given project. Not sure how to do the same with the documentation though.
If you are using meclipse do
window --> maven --> Download Artifact Sources (select check)
(If you still get attach source window, then click on attach file button and close the attach source window. The next time you try to see the source it will open the correct source)
There is also a similiar question that answers this and includes example pom settings.
I tried windows->pref..->Maven But it was not working out. Hence I created a new class path with command mvn eclipse:eclipse -DdownloadSources=true and refreshed the workspace once. voila.. Sources were attached.
Source jar's entry is available in class path. Hence new build solved the problem...
in my version of Eclipse helios with m2Eclipse there is no
window --> maven --> Download Artifact Sources (select check)
Under window is only "new window", "new editor" "open perspective" etc.
If you right click on your project, then chose maven--> download sources
Nothing happens. no sources get downloaded, no pom files get updated, no window pops up asking which sources.
Doing mvn xxx outside of eclipse is dangerous - some commands dont work with m2ecilpse - I did that once and lost the entire project, had to reinstall eclipse and start from scratch.
Im still looking for a way to get ecilpse and maven to find and use the source of external jars like servlet-api.
Changing pom for maven-eclipse-plugin to include source/javadoc just apply for new dependencies being added to pom. If we need to apply for existing dependencies, we must run mvn dependency:sources. I checked this.
Checking download source/javadoc in Eclipse-Maven preference, sometimes is not enough. In the event maven failed to download them for some reason (a network blackout?), maven creates some *.lastUpdated files, then will never download again. My empirical solution was to delete the artifact directory from .m2/repository, and restart the eclipse workspace with download source/javadoc checked and update projects at startup checked as well.
After the workspace has been restarted, maybe some projects can be marked in error, while eclipse progress is downloading, then any error will be cleared.
Maybe this procedure is not so "scientific", but for me did succeded.
I've added the pom configuration to the maven-eclipse plugin to download source and javadocs, but I figure/hope that will happen for new dependencies, not existing ones.
For existing dependencies, I browsed in package explorer down to the "Maven Dependencies" and right-clicked on commons-lang-2.5.jar, selected Maven | Download Sources and... nothing appeared to happen (no progress bar or indication that it was doing anything). It did, however, download as I'm able to jump to source in commons-lang now.
overthink suggested using the setup in the pom:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plgins>
</build>
...
First i thought this still won't attach the javadoc and sources (as i tried unsuccessfully with that -DdownloadSources option before).
But surprise - the .classpath file IS getting its sources and javadoc attached when using the POM variant!
For Indigo (and probably Helios) the checkboxes mentioned above are located here:
Window -> Preferences -> Maven
I had a similar problem, and the solution that worked best for me was to include the source in the same jar as the compiled code (so a given directory in the jar would include both Foo.java and Foo.class). Eclipse automatically associates the source with the compiled code, and automatically provides the JavaDoc from the source. Obviously, that's only helpful if you control the artifact.
After Setting the Properties either at Project Level or User Properties level,
Please do a Maven -> Update Project (Force Update). It downloads the sources
A Small addition to the answer, if your project is not a maven project still you can get the source code of the jars, by using this plugin provided in eclipse
Java Source Attacher