Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Java API's?

Tags:

java

class

api

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?

like image 994
ritch Avatar asked Dec 21 '10 21:12

ritch


3 Answers

Typically APIs are packaged in so-called JARs, which stands for Java ARchive. What you should do is:

  1. Download the jar.
  2. Put it in some sort of 'lib' directory in your project structure. Do not put it with your source code.
  3. 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.
  4. 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.

like image 59
hvgotcodes Avatar answered Oct 16 '22 01:10

hvgotcodes


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.

like image 43
LINEMAN78 Avatar answered Oct 16 '22 01:10

LINEMAN78


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).
    1. Option 1, add an external jar:
      1. Open the project properties in Eclipse
      2. Select Java Build Path
      3. Select the Libraries tab
      4. Click the Add External Jar button
      5. Browse to the location of the JAR in question.
      6. Select the JAR.
      7. Click the Open button.
    2. 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.
like image 25
DwB Avatar answered Oct 16 '22 01:10

DwB