Using java library in eclipse - eclipse

I'm a bit new to eclipse and want to use the following libraries so that I can use their implemented objects (HttpClient and Java csv). How do I import these libraries so that I can write some java with them?
http://hc.apache.org/downloads.cgi
http://sourceforge.net/projects/javacsv/

What you're looking to do is add the libraries to your project's build path (the class path that will be used while compiling). In Eclipse, you can do this by right-clicking your project and choosing Properties (or hitting [Alt]+[Enter] when the project is selected in Project Explorer, Navigator or Package Explorer views) and then Java Build Path from the sidebar and the Libraries tab where you can add JARs.
Note the difference between Add JARs and External JARs is that External JARs will add an external dependency in your project since the absolute path to the JAR on your filesystem will be put into your project's configuration. With Add JARs you can select JARs from within your workspace.
I find it to be a good practice to create a lib folder (at the same level as my src folder) and put all my JARs in there and then add them to the build path with the Add JARs option. This makes the project portable since there are only relative paths referring to resources within the project rather than absolute paths or resources from other workspace projects.

You add the .jar files you want to use in your projects build path. You access this windows by right-clicking your project. Choosing "Build path" -> "Configure build path".

Related

Netbeans to Eclipse migration for Java Projects

I currently switched to eclipse and trying to migrate my projects. I created a java-workspace and used 'File->Open Projects from File System'.
The project with all the folders is added, not only the 'src' but everything I put into it (datasheets, documentation,...). Also the libraries are added two times. One time in the folder and what seems like a link to the compiled library.
folder structure
In netbeans I just added the desired library to the /libs folder and linked it to the project.
Can I manually add folders and/ or libraries to existing projects? Why are there two instances of the libraries?
By using Open Projects from File System, folders containing .java files has been configured as source folders (source code intended to be compiled). This can be undone via right-click Build Path > Remove from Build Path (the reverse function is Build Path > Use as Source Folder).
In the Package Explorer, source folders are shown as virtual folders. If the source folder is not a subfolder of the project, it is displayed as virtual folder in addition to the project subfolder to be able to navigate to that non-source project subfolder (in your case you have a single src source folder and in addition to the non-source folder lib you have multiple virtual lib/... source folders).
The node Referenced Libraries lists all JARs and class folders on the Java Build Path (classpath/modulepath). To remove something from the Build Path, right-click it and choose Build Path > Remove from Build Path. JARs can be added to the Build Path by right-clicking the JAR and choosing Build Path > Add to Build Path. Class folders can be added only via Project > Properties: Java Build Path in the tab Libraries with the button Add Class Folder....
I dont know if this is a workaround or good practice for migration:
Create a new Java-Project in Eclipse
add desired Folder structure (including /libs)
In 'Project->Properties -- Libraries' add libraries manually
Clean and Build Project
I have to test functionality and stability but it seems ok.

Eclipse Java Maven Project Java file which is not on a build path

I met a problem:
when I double click java files in a Maven project the JDT icon tells its not on a build path,
and if I double click java files in normal project it would be normal and I can use ctrl+click to trace the declaration of classes.
How do I set the build path right for those java files in Maven projects?
In maven projects, all the files inside the folders src/main/java and src/test/java are in the build path. Usually, there is no need to change that. You should use other folders only in special cases.

How to add a library to eclipse of .jar files that are NOT a "User Library"?

I am attempting to add a group of .jar files to my build path for a project that is under source control. Specifically I have downloaded the Smack API and I have a folder located in my project/libs/ that contains the multiple smack .jar files I will need.
If I go to Project > Properties... and try to use "Add Library..." under the "Libraries" tab. The only option that makes sense is to add an "User Library". However, this doesn't work with source control as User Libraries appear to be defined in the workspace/.metadata/ folder, and is not under source control.
I can add each .jar file using Project > Properties... using "Add JARs..." under the "Libraries" tab, but then each .jar shows up separately in the build path (not grouped neatly together like other system libraries). Between HAPI, HTTP Components, and Smack .jar's I need, the build path contains a lot of separate jar files and I just want to group them together.
Is this a limitation of Eclipse? Or am I missing something?
Here's the link from one of my Eclipse projects. Eclipse does not group them together - you just add each jar and get the list from this screen. To add libs that are in the project folder structure, use Add Jars..., for external jars, use Add External Jars...
As mentioned by #mikemil, add external jar is the way for non-web apps.
If your project is a web application and has a .../WEB-INF directory, you can put the jar in the .../WEB-INF/lib directory and it will be in the deployed WAR file as well as in the build path.

Can I add predefined jars to WEB-INF/lib in Eclipse project?

When I am designing normal Java project in Eclipse, I can add predefined libraries to it's Build Path. Can I do the same way when putting jars into WEB-INF/lib folder of Web project? I.e. can I ask Eclipse to put some predefined library jars there?
Just copy / import a Jar there; it will be added to the build path automatically. You will find it in the package explorer within the Web App Libraries (or in the build path in the project properties). The project has to have the Dynamic Web App nature.
This is what I did. I copied the jar files into my WEB-INF/lib folder. Then, I added these jars into my build path using: Properties -> Java Build Path -> Add Jars -> Select the jars from the WEB-INF/lib folder.

Adding jars to a Eclipse PlugIn

I try to build a Eclipse plugin that has to use a self written jar which is dependent on other jars, but I don't get the point where to start with handling jars as seperate PlugIns. Anywhere I have to use just the .jar files or am I wrong?
I think I found a proper solution; the trick is that you have to implement all the files via Eclipse. I just copy here the solution which was posted to news.eclipse.platform:
Include the jars in a plugin:
Use Import > File System to import the jar files into your plugin project, say in the <project>/lib directory.
Use Add... button to add the jars to the classpath section of the plugin.xml > Runtime tab.
Use New... button to add "." library back (with no quotes, of course).
Make sure your binary build exports the new jar files on the plugin.xml > Build tab.
Save
On the project, use context menu > PDE Tools > Update Classpath to correctly add the jars to the eclipse project classpath.
What is a self-written jar?
Normally you turn 3rd party jars into bundles using an OSGi MANIFEST.MF (See New>Plug-in Development>Plug-in from Existing JAR archive) or you include them in your plugin.jar and add extra Bundle-ClassPath entries as mentioned by TomaC.
If you mean at runtime your plugin will create a new jar and needs to load it, that's different, though.
Project Properties -> Java Build Path -> Add External jars. Is this what you are looking for?