How to use Java API's? - java

Probably really simple but I just cannot work out how to use any API's with Java (Using Eclipse).
Where do they go?
For example I want to use a Twitter API and I import it using:
import net.unto.twitter.Api;
I then include the API file in the same dictionary as my class file. (This is what I do in python)
Anyone care to tell me the simple answer?

Typically APIs are packaged in so-called JARs, which stands for Java ARchive. What you should do is:
Download the jar.
Put it in some sort of 'lib' directory in your project structure. Do not put it with your source code.
Add the jar to your classpath. Some IDEs you have to add jar by jar, others you tell the IDE which directories are library directories and it will modify the classpath for you to include all the jars in the directory.
Import the relevant package in your files where you use the API.
Once you have the jar added to your classpath and import the relevant classes, Eclipse should automatically pick it up and allow you to use autocomplete features against the API.
It is also a good idea to get the source jar for any libraries you download. Most IDEs let you attach src jars, so you can click into the API and see how the code is written.

You need to include any external JARs in the build path. In eclipse right click on the project and go to 'Build Path' > 'Configure Build Path' then go to the 'Libraries' tab and 'Add JARs'. Also, when using eclipse it is easier not to maintain the import statements yourself, but instead use 'Organize Imports' (ctrl + shft + o) or Import the selected object (ctrl + shft + m). If a matching object is available in the classpath it will be imported.

Java APIs are distributed in JAR files (which are just zip files with a specifiec directory structure).
Download the jar in question, put it in a known location on your disk (known location as in, pay attention to the name of the direcctory where you store the downloaded jar).
There are two times you will need to reference a Jar file:
During project compilation. This includes development of the project using an IDE like eclipse (they all compile the code to find errors).
Option 1, add an external jar:
Open the project properties in Eclipse
Select Java Build Path
Select the Libraries tab
Click the Add External Jar button
Browse to the location of the JAR in question.
Select the JAR.
Click the Open button.
Option 2, add the JAR to your web project's WEB-INF/lib directory.
During project execution. This will be after you deploy your application to a web container (perhaps tomcat). Store the JAR in question in the classpath of the appliction post deploy. For simple web applications, you can put the jar in the project WEB-INF/lib directory.

You should have it included in the classpath (build path) of the project.

Related

Eclipse error "the import com.microsoft cannot be resolved" [duplicate]

How do I import a jar in Eclipse?
You can add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar. A quick demo here.
The above solution is obviously a "Quick" one. However, if you are working on a project where you need to commit files to the source control repository, I would recommend adding Jar files to a dedicated library folder within your source control repository and referencing few or all of them as mentioned above.
Adding external Jar is not smart in case you want to change the project location in filesystem.
The best way is to add the jar to build path so your project will compile if exported:
Create a folder called lib in your project folder.
copy to this folder all the jar files you need.
Refresh your project in eclipse.
Select all the jar files, then right click on one of them and select Build Path -> Add to Build Path
Two choices:
1/ From the project:
2/ If you have already other jar imported, from the directory "References Libraries":
Both will lead you to this screen where you can mange your libraries:
Here are the steps:
click File > Import. The Import window opens.
Under Select an import source, click J2EE > App Client JAR file.
Click Next.
In the Application Client file field, enter the location and name of the application client JAR file that you want to import. You can click the Browse button to select the JAR file from the file system.
In the Application Client project field, type a new project name or select an application client project from the drop-down list. If you type a new name in this field, the application client project will be created based on the version of the application client JAR file, and it will use the default location.
In the Target runtime drop-down list, select the application server that you want to target for your development. This selection affects the run time settings by modifying the class path entries for the project.
If you want to add the new module to an enterprise application project, select the Add project to an EAR check box and then select an existing enterprise application project from the list or create a new one by clicking New.
Note: If you type a new enterprise application project name, the enterprise application project will be created in the default location with the lowest compatible J2EE version based on the version of the project being created. If you want to specify a different version or a different location for the enterprise application, you must use the New Enterprise Application Project wizard.
Click Finish to import the application client JAR file.
Just a comment on importing jars into Eclipse (plug-in development) projects:
In case you are developing Eclipse plug-ins, it makes sense to use Eclipse's native bundling mechanism instead of just importing the jar into a plug-in project. Eclipse (or better its underlying OSGi runtime, Equinox) uses so-called bundles which contain some more information than plain jars (e.g., version infos, dependencies to other bundles, exported packages; see the MANIFEST.MF file). Because of this information, OSGi bundles can be dynamically loaded/unloaded and there is automatic dependency resolution available in an OSGi/Eclipse runtime. Hence, using OSGi bundles instead of plain jars (contained inside another OSGi bundle) has some advantages.
(BTW: Eclipse plug-ins are the same thing as OSGi bundles.)
There is a good chance that somebody already bundled a certain (3rd party) library as an OSGi bundle. You might want to take a look at the following bundle repositories:
http://www.springsource.com/repository/app/
http://download.eclipse.org/tools/orbit/downloads/
http://www.osgi.org/Repository/HomePage
Eclipse -> Preferences -> Java -> Build Path -> User Libraries -> New(Name it) -> Add external Jars
(I recommend dragging your new libraries into the eclipse folder before any of these steps to keep everything together, that way if you reinstall Eclipse or your OS you won't have to rwlink anything except the JDK) Now select the jar files you want. Click OK.
Right click on your project and choose Build Path -> Add Library
FYI just code and then right click and Source->Organize Imports
Jar File in the system path is:
C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar
ojdbc14.jar(it's jar file)
To import jar file in your Eclipse IDE, follow the steps given below.
Right-click on your project
Select Build Path
Click on Configure Build Path
Click on Libraries, select Modulepath and select Add External JARs
Select the jar file from the required folder
Click and Apply and Ok
first of all you will go to your project what you are created
and next right click in your mouse and select properties in the bottom
and select build in path in the left corner and add external jar file add click apply .that's it
In eclipse I included a compressed jar file i.e. zip file. Eclipse allowed me to add this zip file as an external jar but when I tried to access the classes in the jar they weren't showing up.
After a lot of trial and error I found that using a zip format doesn't work. When I added a jar file then it worked for me.
Right Click on the Project.
Click on Build Path.
Click On Configure Build Path.
Under Libraries, Click on Add Jar or Add External Jar.
If you are having a trouble on udemy course of chad on springboot for the importing the jar. Then follow these steps.
Right click on the project.
You will see a option of Build Path, click on it.
You will have a option of Configure Build path, click on it.
Go to libraries.
Then go to the place where you have a jar files make them into a new folder and upload on a new place.
Then click on Add External Jars, you will surely be able to upload it which will help on classpathxmlapplicationcontext as well.
Thank you.

Where to put Java libraries?

I'd like to know how I can add Java libraries to an Eclipse project on a development machine so that they can be added to an Eclipse project without causing errors when someone who has the library in a different location. For example, one developer might add an external JAR in C:\Java, but another might have the same JAR somewhere else. (Where's C:\Java on Mac OS?) I thought I might set the CLASSPATH environment variable, but I can't figure how to add an external JAR from the CLASSPATH environment variable. I'd like to do this so that it works with any workspace. Is this possible?
This is specifically for use with Anypoint Studio, but I think the same problem would exist with any Eclipse-based IDE.
In general, it's recommended to either embed JARs directly into the project, usually in a /lib folder of the project, as described here; or to use a tool like Gradle or Maven to manage dependencies, both of which have nice plugins to support their use in Eclipse.
Another alternative would be to use a Classpath Variable to refer to the JAR(s), which abstracts the physical location so that it can be set on a per-workspace basis.
I think the best way to add library to eclipse project is creating a directory - lib in your project directory. Then add the whole lib to you eclipse class path. You can follow these step to add a lib to class path -
Right click on project and select properties
Select Java Build Path
click Add Library and create User Library
Now add External Jars to this library create at step 3.
By this a .classpath file is crated in you project directory and the CLASSPATH problem will be resolved
I guess the best way to do that would be using Maven, or a similar build system that can construct your Classpath base on dependencies.
You can add the dependencies to your pom and having the jars in your local maven repository in the machine.
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
Because in any other approach you will need to maintain everything manually, and when having different OS the path will change.
Eclipse is just the IDE that will help to write code and assemble the project (JAR, WAR, ... ). You can add your external jars from wherever you want, and when you want to export your project (with eclipse) you may choose to package the required libraries into the jar.
However, I recommend always to use maven (or something like ) to avoid this kind of problems.
Part1:(import .jar file as library in Eclipse)
You make a new project to Eclipse(name:Project1)
When you open it you see JRE System Library[java version something]
1.right click on JRE System Library
2.Go->Build Path->Configure Build Path
3.You can see (Up right Corner the button[add jars or add external jars]
*Here i advise you to choose the first(add jars) but..
*First copy(or move) the (name).jar inside the project((example):Project 1)
*Now you can add it with the button(add jars).
*In this way when you finish your project the (name).jar will be
imported inside the project(If you export it as a .jar from Eclipse)
..Now you can call any method of (name).jar just(import it into the class
you want to use and call it)

Can I refer to a Java source path using a variable in Eclipse

Background
I'm using an Eclipse 4.2 (Juno) release to build a plugin for a Java application. The source code and classes for my plugin all reside within the project workspace. The application jar and its source code are, for various reasons maintained outside of the eclipse workspace and the application jar is produced by another build mechanism.
This isn't a problem as I have referenced the application jar file in my project using drag and drop and the 'Link to files' option specifying it 'Create link locations relative to: MYDEV', where MYDEV is a Linked Resource Variable I have created for each Eclipse installation. This lets me use the same eclipse project on multiple machines where the path to the application jar varies, but is always the same relative to MYDEV.
However, I cannot find a way to associate the application source code with this jar unless I use an absolute path. I only want access to the application source for debugging purposes.
Question
In Eclipse, how do I attach java source to a referenced jar in a way that allows a project to be used on multiple installations where the referenced source code has been relocated?
If the source is outside the proyect, then there's no way to access it without an absolute path.
The best solution is using a symlink inside the eclipse project directory to the actual source folder; eclipse won't notice the source is outside the project directory and everything will work fine, without having to relocate the source.
This is the only way I was able to do what you are suggesting (do not know if there are better ways).
Package a JAR file that contains the source code of the JAR file. Place it inside your project in a folder (you do not need to add it to your class path)
In the eclipse project right click on the JAR file and go to "properties" and then to "Java Source Attachment" From there select "Workspace" button and pick the jar file with the source code we added from the above step.
So the source attachment path will always be relative to the project. That way if you share the project via SVN, GIT or whatever, the source will always be available and it'll work if the user is running eclipse on Windows or Linux.
I've had a similar problem to you where absolute paths were a problem for people using different OS and not even using mapped drives via Samba helped that much.
Hope this helps.
The way I have solved this problem is to use a use defined library in eclipse. Here the steps that you can use to solver this problem.
Create a simple project in eclipse in the directory that contains the jars and the source code files.
Define a java user library add the jars to the user library and for each jar in the library specify the location of the source. If the source files are in a project that is in the workspace then the path will be relative to the workspace folder.
Export the user library as an .xml file
When another use wants to setup an eclipse workspace with the same setting as yours they will do two things.
Import the project that contains the jars and the sources into their workspace.
Import the user library into their workpsace.

After building the project from pom.xml using Maven, how do I use its resources

It's been tedious. This is the API I am trying to use. Its resources were set up in a pom.xml which I built using Maven. On built up, it gave me the project socrata-publisher that has
src/main/java the source folder with packages com.socrata.api com.socrata.data, com.socrata.util where each contains only .java
files
JRE System Library and Maven Dependency hierarchies where each contains a number of jar files
Problem is com.socrata.api and the 2 other contains classes which I want to deploy in a project outside socrata-publisher. I tried using import com.socrata.api but it didn't work. Moreover, since its a Java project and not android it doesn't have the is Library option in preferences which could rather give me the solution. Both socrata-publisher and tutorial (where i want to use the resources and which is the android application) lie in the same directory eclipseApps in My Documents.
Here's a little visual queue. Help will be greatly appreciated.
You will need to first of all get the output of the socrata project and all its dependencies.
In command line, going to the project folder of the socrata project, where the pom.xml file is, run MVN INSTALL. You shall see a jar file called socrata-api.jar in $HOME/.m2/repository. If you are using windows and installed MAVEN by default, $HOME should be your user profile folder. Once you see the jar file, add it to your tutorial build path.
I think what you actually want to do is just set up the "socrata-publisher" as a project dependency for your "tutorial" project. That will allow you to reference the built Socrata libraries from the code in your project.
Right click on the project and select "Properties". From within that dialog select "Java Build Path" on the left, then the "Projects" section, and click the "Add" button to add the "socrata-publisher" project.
I think that'll work better than creating a separate jar file that you then include, and then you can also keep the socrata-publisher code up to date using Git.

How to add source files of library jars?

I am using Eclipse IDE and its derivative like Spring IDE for Java development.
In a web application project, I add external jars like Spring MVC jars, Apache commons jars etc to the Web App library folder, hence they are automatically added to the build path. There are many jars in the Web App library folder.
I want to create folder in the project and add all the source files (zip/jar) of the libraries included in Web App library folder, so that I can navigate through the source of libraries from the Java editor window. Whenever I add a source zip/jar file to this folder, Eclipse should detect it and use it whenever I want to navigate to the source of a library.
Is the above possible in eclipse?
Note: I know how to add source files
for each individual jar by navigating
to the build path window and
specifying the source location. But
this is very crude way, and I need to
do for every library individually.
Also the drawback is that source path
is absolute, which means if I import
the project into another computer then
I need to create the source path or
even worse I might have to add the
source files individually again.
One way to automagically get the sources for the jars would be some kind of dependency management system. Most people would scream Maven (2/3) by now, but others exist and work well. Maven does have nice Eclipse integration, so that should be a plus.
The downside is that setting up a Maven project just for it's dependency management can seem overkill. Another point is that all the jars you depend on should be "Mavenized" as well.
As far as I know Eclipse wont automatically detect/scan source archive files and link them up to libraries in your workspace in the way you described it.
I agree with #Gressie on using Maven and the Eclipse Maven plugins -- as in that case it's just a matter of ticking a few boxes and Maven will do that for you.
If however your project is not Maven-ized, you can still do this in Eclipse but it's more tedious:
for each one of the jars in your project (which appear under the dependecies section) right click on it and select properties
in the dialog that pops up you have (at least) 2 locations you can configure: java source attachment -- simply browse to your jar with the sources -- and also javadoc location (point it to the jar with javadoc if you want the javadoc to appear as a tooltip when you hover the mouse over one of the classes/methods/etc in that library).

Categories