Step-By-Step Guide to Creating and Inserting a Java API in Eclipse

Eclipse is the most popular Java Integrated Development Environment (IDE). Just check out the survey result.

**Image Source: **IProgrammer

Here is one more for you.

Image Source Qiita

Some people say that Eclipse is dying. However, we can see that many developers still prefer using Eclipse. Let’s get Eclipse popularity part out of the way and talk about how you can insert a Java API in Eclipse. You can do it quickly. But before getting to adding and creating API, I will educate about Java API for readers who do not have in-depth knowledge of Java API.

What is Java API?

The full form of API is Application Programming Interface (API). Java language comprises various syntax and semantics. The set of classes that are inside the Java Development Environment is known as Java API.

The classes in Java API is in Java language, and it runs via a Java Virtual Machine. You can find every single thing inside Java API, ranging from collection classes to GUI classes.

To get the most out of Java API, you need to have a firm grasp of Java programming language. Java is the most popular language in the world today, and huge corporations love Java due to its great documentation and security features.

If you want to land a project in Java, it is beneficial to undertake a Java Certification Training course that gives clear insights about various aspect of the language being a big plus for novice as well as tech professionals.

There are various levels in Java certification courses. You can slowly jump from one level to the another and learn as you go. You can write some codes on the side while going through the Java certification courses.

How to Create and Insert a Java API in Eclipse?

Now that you are aware of Java API, we can talk about the process of creating and inserting a Java API in Eclipse.

To create a great program without wasting much time, you need to work in a framework. If you are among the developers who are looking to develop your API via Eclipse, you are in the right place. I will show you a step-by-step guide to creating and inserting a Java API in Eclipse. All you need to do is systematically follow my instruction.

Project Creation

The first task is a project creation. Here are the steps:

  • Enter the current workspace inside Eclipse. It is better to create a new workspace to ensure that you do not mess your projects.
  • After the first step, you need to make a new Java Project and give whatever name you like. However, you should make sure that your name relates to your project.
  • After the project creation, you need to test if you can see your project in an explorer window or not. It is essential to make sure that it does not mess around with other projects.

Fresh Package and Class

  • Head to project explorer and right click your project. After selecting a new package, name it. It will act as the main class file.
  • Head to package, click new, and then select class under the source folder. Give a relevant name to the class. Make the class, public and do not make the main method stub in class.
  • After the completion of the first two steps, double-click your class file.

Time to Add Code and Export

  • You can now add some codes to your project. For now, create some random static methods that return something.
  • When you finish adding methods to your class, export it as a jar file. It is a simple process. Go to the project source folder and select Export. Then click on Java and select JAR file. Note down the export location, so that you will be able to access it later. You can then click on the Finish button and let the Eclipse do the rest for you.

Do the Testing

  • You are now all set to do some testing. Just create a project like in step number 1. Name it different this time around.
  • There is no need to export it because it is a test project. Instead of exporting, go with creating a package. Just create a new class and name it “test.” You can now inherit the public static void main method. Select the finish button after completing the process.
  • You will observe a class file with the primary method when you smash the finish button. In case you fail to see the class file, copy the code below.

Create a JAR for Building a Path

You are in the ending part of creating and inserting a Java API. You have a primary method ready. Now, you should add API to a build path of the new project.

  • Select your new project and then go to properties.
  • Select “Java Build Path” on the properties–>window.
  • Select Libraries after entering the “Java Build Path” in the properties menu.
  • Click on “Add External JARs..” and go to your API location and then select open.
  • Click the open button to finish the step number 5.

Time to Run your API

You are finally ready for a climax. You have now created your API. What to do next? Just implement your API and observe your computer screen. If there are errors, go through the article once again to see if you missed something along the way.

Over To You

Did you find it challenging to create and insert Java API to Eclipse? Hopefully not. It is so easy that you can finish the task of creating and adding Java API to Eclipse within 10-30 minutes. After the installation of Java API, you can complete your projects in a shorter period.

Remember, smart programmers not only work hard, but they work smart as well. Creating and inserting Java API to Eclipse is the first step to working smart. I hope you have found value in this article. If you have any confusions, feel free to comment below. I will be more than happy to assist you in your path to getting things done.

How do I set the default Java (JDK) version on Mac OS X?

In this post you will learn how to set the default JAVA_HOME in Mac OS X when you have more than one JDK installed in your computer. First you need to run /usr/libexec/java_home -V command to get the list of installed JDK. The command will print out something like the following depending on the available JDK in your computer.

On my machine I have the following version of Java.

Matching Java Virtual Machines (3):
    9, x86_64:  "Java SE 9"     /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
    1.8.0_121, x86_64:  "Java SE 8"     /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
    1.7.0_80, x86_64:   "Java SE 7"     /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

From the list above pick which version you want to be the default JDK. For example, I will choose the 1.8.0_121 version to be my default JDK. To set it run the command below.

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_121`

If the major version of the available JDK is unique you can just use the major version, like:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

After setting the JAVA_HOME and you run the java -version command you will see that JDK 1.8 is the new default JDK in your computer.

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

The change above will only be active in the current running shell. If you close or terminate the shell, next time you open the shell you will need to set it again. To make this change permanent you need to set it in your shell init file. For example if you are using bash then you can set the command in the .bash_profile. Add the following lines at the end of the file.

# Setting default JDK to version 1.8.
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

To activate this configuration right away your can run source .bash_profile. This command reads and executes the .bash_profile in the current shell.

How do I pass password to sudo commands?

If you want to run a sudo command without being prompted to input the password you can do the following command.

echo password | sudo -S rm -rf /opt/jetty/

In the command above we are trying to remove the /opt/jetty directory using the rm -rf command. The -S (stdin) option allow the sudo command to read password from a standard input instead of a terminal device.

If you want to store the password in a file you can use the cat command instead of echo like the following example.

cat password.txt | sudo -S rm -rf /opt/jetty/

How do I install third-party libraries in Maven repository?

Sometimes when the required libraries / dependencies is not available in the Maven Central Repository we need to manually install it to our local repository. This library must be placed in the correct directory in our local repository to enable Maven to find it. The default location is under the ${user.home}/.m2/repository.

To make this job easier Maven provides a maven-install-plugin that will help us to install the third-party library in the correct place. The following command shows how to do it.

The long command

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
        -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

Where:

  • -Dfile = path to the third-party library file
  • -DgroupId = the groupId of the library
  • -DartifactId = the artifactId of the library
  • -Dversion = the version number of the library
  • -Dpackaging = the library packaging

An example to install an Oracle JDBC library to your local repository is:

mvn install:install-file -Dfile=ojdbc7.jar -DgroupId=com.oracle \
        -DartifactId=ojdbc7 -Dversion=12.1.0.2 -Dpackaging=jar

The simple command

If you have the pom.xml file, you can install it with the following command:

mvn install:install-file \
        -Dfile=<path-to-file> \
        -DpomFile=<path-to-pomfile>

Where:

  • -Dfile = path to the third-party library file
  • -DpomFile = the location to the library pom.xml file

Starting with the Maven version 2.5 you can use even a simpler command. When the library is build by Maven, a pom.xml file will be placed under the META-INF directory. This pom.xml file will be used by default when we install the library. To install a library all you need is the following command:

mvn install:install-file -Dfile=<path-to-file>

How do I run FTP server in Mac OS X?

I need to test FTP client codes, so I need to find an FTP server for testing my codes. After searching for a while I find out that OS X already equipped FTP server. I am currently using OS X El Capitan 10.11.*.

Let’s now test the FTP server on Mac OS X with the following steps:

  • Launch the Terminal.app
  • Type the following command to start the FTP server.
sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist
  • Connect to the FTP server by running ftp localhost command.
  • We’ll be asked to enter the username and password.
$ ftp localhost
Trying ::1...
Connected to localhost.
220 ::1 FTP server (tnftpd 20100324+GSSAPI) ready.
Name (localhost:wsaryada): wsaryada
331 User wsaryada accepted, provide password.
Password: 
230 User wsaryada logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
  • If we see the messages above and the ftp> prompt means that the FTP server works and ready to accept our command.
  • We can also try to access the FTP server using a browser. In the URL box type `ftp://localhost` to connect. We need to supply username and password to login.
  • To exit or close the connection to FTP server we can run the exit command.
  • Finally, to shut down the FTP server we run:
sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist

After the FTP server ready, I can now continue to create some test program to access the FTP server. There are already some examples you can find in the Apache Commons Net category that use the FTPClient library to access FTP server.