Introduction: Implement Your Own API in Java Using Eclipse

Picture of Implement Your Own API in Java Using Eclipse

A while back when I was first learning Java, I had always wondered how big games, systems, or programs were created such as Call of Duty, Microsoft Office, or Windows. As my skills in Java and my knowledge about programming in general grew, I started to gain a better understanding of how it all tied together.

When looking at games such as Call of Duty, it wasn't all programmed standalone, many references / interfaces went into the development of the game. It implements DX9 / DX11 for multimedia purposes, Microsoft XNA for runtime purposes, and a few other things to help the game run the way it does now. Almost all large projects are built around a framework such as the examples above.

If you are interested in implementing your own API in Java through Eclipse, this is the right instructable for you. I do not recommend this instructable if you cannot program in Java or use Eclipse, this is for advanced java developers with any skill level using Eclipse. This project will take you around 10 - 30 minutes to complete while reading this instructable. I will not be using many visuals in my instructions, just to give you a general sense where we are at that given time. Make sure you take your time if you have troubles using Eclipse and make sure you don't mess up!

If anything that I say doesn't make sense, take note that I've only been working with Java for around 2 years now, so my knowledge does not amount up to professional developers. This is my honest understanding of implementing an API that you make into another project that you will also make. If you see anything wrong with what I wrote here, feel free to comment below so I can make the appropriate corrections.

Step 1: Creating the Project

Picture of Creating the Project

1. Begin by opening Eclipse and browsing to your current workspace. I will create a new workspace so the project hierarchy isn't clogged up with projects.

2. Once you are in Eclipse, create a new Java Project and give it a name. Before confirming the name, take note that the project you are about to create is for the actual API you will be implementing later on.

3. Once your project is created, be sure you can see it in your project explorer window. This step is to make sure you don't mix up any of your other projects with this current one.

Step 2: Creating the Package and Class

Picture of Creating the Package and Class

1. Begin by right clicking your project under the package explorer, browsing to new, then package. Once you have selected new package, call it something along the lines of com.[your_name_here].[project_name_here], this package will house the main class file you will implement later on.

2. Right click the package you just created located in the source folder under the package explorer, browse to new, and click Class. After making a new class, give it a name relevant to your project, so I'll call mine Example. Be sure you do not create the main method stub in the class (only inherit abstract methods). Be sure the class is public and it's superclass is java.lang.Object

3. Once all is done, double click on your class file in the package explorer. Be sure that once you do, it displays the empty class file in the center window of Eclipse.

Step 3: Code and Export

Picture of Code and Export

1. It's time to add some code to this lovely class! Lets begin by creating two random static methods that return something other than void. I'll make two random methods, you may do the same!
(My class file after methods were created: Click here)

2. Once you added some methods to your class file, you're ready to export this as a jar file! Begin by right clicking your projects src folder and selecting export. Once in the export window, select Java -> JAR File as seen in the image above. Select next and browse to the location of export. Try to export a location that you will remember because this jar will be implemented later on! Be sure your settings meet mine (located in the image above) other than the export location. Once all is well, click on Finish and let the Eclipse compiler package up and export your jar file!

Step 4: Test Project

Picture of Test Project

1. Now that we're done exporting the API, it's time we create a project to test it! Simply create a new project as you would in step 1, but this time call it something other than what you had before. I'll call mine TutorialProject because that's all it will ever be good for.

2. Since this is a test project and it won't have to be exported, instead of creating a package this time, I will just create the class file, which will generate a default package. As for the class name, it should be called something along the lines of Test. ** NOTE **: Inherit the public static void main method stub this time! After you have verified that the main method stub will be inherited, select the finish button.

3. Once you select finish, you should see a class file with a main method (as seen above). If you do not, insert the following code into your class file.

public static void main(String[] args) {

}

## TODO COMMENT IS NOT REQUIRED AND SHOULD BE DELETED ##

Step 5: Add JAR to Build Path

Picture of Add JAR to Build Path

Now that we have a main method, it's pretty important that we add our API into the build path of this new project. To do so, follow these steps exactly as they are.

1. Begin by right clicking your new project (for me it's TutorialProject), then select properties.

2. Within the properties window, click on "Java Build Path" located to the left of the properties window.

3. Once inside the "Java Build Path" part of the properties menu, select Libraries in the upper middle of the properties window.

4. Select "Add External JARs...", and browse to the location of your API, then select the file and click open.

5. After all of this is complete, select the OK button located in the lower right of the properties window.

Step 6: Put Your API to Use!

Picture of Put Your API to Use!

1. After the API you created is added to the build path, you are ready to implement it. Simply call one of the methods you created! For me, I will run...
----------------------------------------------------------------------------------------------------------
    System.out.println(Example.isSocketOpen("instructables.com", 80));
----------------------------------------------------------------------------------------------------------
and hey, it seems that instructables.com is up! Thanks for going through my instructable and learning something new, maybe.

Comments

AnshikaG6 (author)2017-09-20

Helpful tutorial!!

I have a question. My API's dont return void but some object. So where and how should I define the classes of those objects and how would the client which use those API's will know about those objects?

KayP32 (author)2017-07-08

Very helpful, thankyou!

I dont think its a problem, that the link to your methods is dead...its not that hard to think of my own methods...dont know why others are so picky about that :D
Created my first own API with your help, good tutorial, thankyou :)

ConnorHollasch (author)2016-12-23

Sorry to everyone who is still using this tutorial, my example class is no longer there as the hastebin expired a long time ago. I don't have the contents inside the class but in general you can use any sort of static reference to the classes in your jar and call that static reference. For good practice, I would create a function that returns the sum of two numbers such as

public static double sumOf(double a, double b) {
return a + b;
}

Then you can reference sumOf in your example class and print the result to test the results.


Thank you for your patience.

AnilK198 (author)2016-12-23

wrong ..

LeighaB1 (author)2016-09-28

Left a few steps out that made this confusing.

KareemullahM (author)2016-06-20

It's great!! Simple and easy to understand. :) .. I helps me to understand two concepts 1. Static Methods and 2. API calls

Here are the Static methods which I have used :

public static int sumOfTwo(int a, int b){

return a+b;

}

public static String helloAPI(String name){

return "Hello "+name+". This is API Sample.";

}

Below are the calls to Static methods from second class:

System.out.println(SampleToAPI.helloAPI("Tony"));

System.out.println("Sum of 23 and 52 is " +SampleToAPI.sumOfTwo(23, 52));

KareemullahM (author)2016-06-20

It's great!! Simple and easy to understand. :) .. I helps me to understand two concepts 1. Static Methods and 2. API calls

Here are the Static methods which I have used :

public static int sumOfTwo(int a, int b){

return a+b;

}

public static String helloAPI(String name){

return "Hello "+name+". This is API Sample.";

}

Below are the calls to Static methods from second class:

System.out.println(SampleToAPI.helloAPI("Tony"));

System.out.println("Sum of 23 and 52 is " +SampleToAPI.sumOfTwo(23, 52));

KareemullahM (author)2016-06-20

It's great!! Simple and easy to understand. :) .. I helps me to understand two concepts 1. Static Methods and 2. API calls

Here are the Static methods which I have used :

public static int sumOfTwo(int a, int b){

return a+b;

}

public static String helloAPI(String name){

return "Hello "+name+". This is API Sample.";

}

Below are the calls to Static methods from second class:

System.out.println(SampleToAPI.helloAPI("Tony"));

System.out.println("Sum of 23 and 52 is " +SampleToAPI.sumOfTwo(23, 52));

RoyL33 (author)2016-06-01

Detailed tutorial, but it would be better if you say "Import JAR as a referenced library in build path configuration". And the link that you provide your methods are empty.

DanielM506 (author)2016-05-12

There is nothing in the link you provide to view your methods. Please can you update this. Thanks!

AnkurS36 (author)2016-04-11

Its helpful if you shared your methods. Useless.

glittercoder (author)2016-01-27

I'm unable to check out the methods you have created. The final implementation of the api is difficult to understand coz of this.... plz fix it ASAP!

IuriM1 (author)2015-12-14

build path?

About This Instructable

101,636views

23favorites

License:

More by ConnorHollasch:Implement Your Own API in Java Using Eclipse
Add instructable to: