Introduction: 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

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

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

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

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

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!

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.