How to Develop and Use a Java API in Eclipse




Introduction: How to Develop and Use a Java API in Eclipse

This Instructable will teach you how to both import and export JAR files in Eclipse. This is a useful skill to have as it will allow you to deploy code for others to use as well as use code others have written for you to use. This process is surprisingly easy and will likely take no more than 10-15 minutes for your first time, even if you have only a basic grasp of Java and Eclipse. After you've done it a few times you'll be able to do it it 1-2 minutes. The steps are a good shorthand, but I highly recommend following along the screenshots as there is one for literally every step and they're much easier to follow.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Create a New Java Project

1. Click File->New->Java Project (Figure 1)

2. Name the project whatever you want your API to be called. (Figure 2)

I call mine Example. All of the default settings should be fine, but see Figure 2 for a detailed example of how things should look.

3. Click Finish. (Figure 2)

4. Right click your newly created project and select New->Package. (Figure 3)

5. Name the package whatever you like and click Finish. (Figure 4)

6. Right click your newly created package and select New->Class. (Figure 5)

7. Name the class whatever you like and click Finish. (Figure 6)

Step 2: Write the API

Write this class the same way you would write any class in Java. Fill it with whatever methods you want using any parameters that you like. Just make sure to include the static keyword in each method declaration. Here are a couple of example methods you can try out.

public static void hello(){

System.out.println("Hello, world!");


public static int return50(){

return 50;


public static int timesTwo(int i){

return (i * 2);


See Figure 1 to see these methods inside the class.

Step 3: Export the API

Our API is written and ready for use, however in order to use the API in other projects we will first have to export it as a JAR file.

1. Right click on the class and select Export... (Figure 1)

2. Inside the Java folder, select JAR file and click next. (Figure 2)

Note: Do not select Runnable JAR file.

3. Expand your project folder, src folder, and package. (Figure 3)

4. Make sure all 3 are checked as well as the class files you want to export in the right window. (Figure 3)

5. Select Export generated class files and resources as well as Export Java source files and resources. (Figure 3)

See Figure 3 to know exactly what should be selected and what shouldn't be selected. The default options are probably wrong here.

6. Select a destination to export the file to. (Figure 3)

This just needs to be somewhere where you can find the file again easily. Later on we're going to import the file so it won't matter where it was so long as you can find it.

7. Click Finish. (Figure 3)

Step 4: Import the API

We've exported our API as a JAR file. Now we need to import it into the project that we want to use it in. Realistically, you won't be exporting your own JAR file and then importing it again into another project. You'll more likely be either exporting an API for someone else to use, or importing one that someone else made for your own use.

1. Create a new Java Project by following the procedure in step 1.

2. Right click a class file in the new project and select Build Path->Configure Build Path... (Figure 1)

3. Select the Libraries tab. (Figure 2)

4. Click Add External JARs... (Figure 2)

5. Navigate to wherever you stored your JAR file and select it. (Figure 3)

6. Click Open. (Figure 3)

7. Click OK. (Figure 4)

Step 5: Use the API

Now we just have to add an import statement and remember to preface the methods we want to use from the API with the name of the class.

1. In between the package line and the class line, add the following statement:

import [package].[class];

Where [package] is the name of the package of the API you're using and [class] is the name of the class. See Figure 2 for a detailed look.

2. Remember to preface the methods you want to use with the name of the class as seen in Figure 2.

We're basically done now. All methods inside the API can now be accessed in this new project. You can view the API to review it's methods by following these steps. (Figure 1)

1. Expand Referenced Libraries

2. Expand the JAR file

3. Expand the package

4. Expand the .class file

5. Double click the class file

Congratulations! You now have the knowledge to import and export code in the form of JAR files. This will help a lot when it comes to collaborating with other developers. If you're having trouble getting things to work, here are a couple of common problems.

1. You forgot to check one or more boxes in Step 3.5.

The correct boxes are not checked by default so this is an easy mistake to make. Try to export it again and look closely at figure 3 to make sure you've got all the correct boxes checked.

2. You forgot to give your methods the static keyword in Step 2.

The static keyword isn't used too often so it could easily slip your mind. Go edit the keyword in and export your API again.

Be the First to Share


    • Trash to Treasure Contest

      Trash to Treasure Contest
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    2 Discussions

    Mark Liamey
    Mark Liamey

    3 years ago

    Interesting article. There are many useful things. I made many wonderful mobile app development at and I'd like to see its for everyone.


    3 years ago

    elegant n simple explanation.tx