How to Develop and Use a Java API in Eclipse
by kendallberner in Circuits > Software
42226 Views, 77 Favorites, 0 Comments
How to Develop and Use a Java API in Eclipse
![06.png](/proxy/?url=https://content.instructables.com/FVF/4GLS/IM53U8LR/FVF4GLSIM53U8LR.png&filename=06.png)
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.
Create a New Java Project
![01.png](/proxy/?url=https://content.instructables.com/FWE/24J5/ILJWFJVS/FWE24J5ILJWFJVS.png&filename=01.png)
![02.png](/proxy/?url=https://content.instructables.com/FLM/7UOB/ILJWFJVT/FLM7UOBILJWFJVT.png&filename=02.png)
![03.png](/proxy/?url=https://content.instructables.com/FZ3/DQKA/ILJWFJVX/FZ3DQKAILJWFJVX.png&filename=03.png)
![04.png](/proxy/?url=https://content.instructables.com/FYC/9TR0/ILJWFJW2/FYC9TR0ILJWFJW2.png&filename=04.png)
![05.png](/proxy/?url=https://content.instructables.com/FM9/F2BL/ILJWFJW3/FM9F2BLILJWFJW3.png&filename=05.png)
![05.5.png](/proxy/?url=https://content.instructables.com/FIQ/YLLQ/ILJSHZGR/FIQYLLQILJSHZGR.png&filename=05.5.png)
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)
Write the API
![06.png](/proxy/?url=https://content.instructables.com/FXE/1JX2/ILJWFJW4/FXE1JX2ILJWFJW4.png&filename=06.png)
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.
Export the API
![07.png](/proxy/?url=https://content.instructables.com/F40/15XG/ILJWFJW7/F4015XGILJWFJW7.png&filename=07.png)
![08.png](/proxy/?url=https://content.instructables.com/F0E/OCF7/ILJWFJWB/F0EOCF7ILJWFJWB.png&filename=08.png)
![09.png](/proxy/?url=https://content.instructables.com/FYE/FMSO/ILJWFJWC/FYEFMSOILJWFJWC.png&filename=09.png)
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)
Import the API
![10.png](/proxy/?url=https://content.instructables.com/F1L/4NT4/ILJWFJWF/F1L4NT4ILJWFJWF.png&filename=10.png)
![11.png](/proxy/?url=https://content.instructables.com/F8Z/3DC7/ILJWFJWG/F8Z3DC7ILJWFJWG.png&filename=11.png)
![12.png](/proxy/?url=https://content.instructables.com/FJM/35R0/ILJWFJWJ/FJM35R0ILJWFJWJ.png&filename=12.png)
![13.png](/proxy/?url=https://content.instructables.com/F5B/R1KM/ILJWFJWL/F5BR1KMILJWFJWL.png&filename=13.png)
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)
Use the API
![14.png](/proxy/?url=https://content.instructables.com/FT7/F4V6/ILJWFJWP/FT7F4V6ILJWFJWP.png&filename=14.png)
![15.png](/proxy/?url=https://content.instructables.com/FHG/H9IX/ILJWFJWT/FHGH9IXILJWFJWT.png&filename=15.png)
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.