SpinSpire logo

Java: From Basics to RESTful Services

Why Java? Hundreds of reasons can be found with a single google search, but we all know Java language is not perfect, and so for every reason for Java, we can find an opposite. But the following two reasons are enough to convince anyone: leading development platform (brings business) and JVM. The Java platform will not go anywhere in the near future, and languages like Groovy will make certain of that. "Groovy is like a super version of Java. It can leverage Java's enterprise capabilities but also has cool productivity features like closures, builders and dynamic typing. If you are a developer, tester or script guru, you have to love Groovy."

So now that you know you can not escape Java, let’s get started! For this tutorial I will be using NetBeans IDE, I will also show you how to manually compile Java code and make JAR files with shell commands. On the way I will also explain some general OOP concepts and Java specific concepts.

Fire up NetBeans and follow the below steps to create a simple Java project:

  • File > New Project,

  • Choose Java from categories, click next

  • Give a project name, i.e. “hello”

  • Click finish

Delete the comments for now. This is how your code should look like:

package hello;

public class Hello {
  public static void main(String[] args) {
  }
}

The first line means that the below class is under the package “hello”. One package will most likely contain many classes. You will discover later why and how packages are used to restrict/allow access to other methods, (known as functions in conventional programming languages) classes, modularity etc.

Public class means that the class basically has no access restrictions, (as opposed to, for example, a private class) it is visible to all other classes that might exist even in different packages. Now what is a class to begin with? You can guess from the word itself, classes are used to group things (objects) of same type. Think of examples such as a class names Animals, for example, will contain various animals, a Car class will contain various brands and models and so on.

Inside our class we have a public static void method. Void means this method will return nothing, so anything that happens in it, happens now and will not be later used by some other methods (for example some sort of a computational result returned). For now the most important thing you might want to know about a static method is that it can be called without an instance of an object (the class). Meaning, all the lines of code inside the class do not have to be run in order to be able to use the static method in other classes. So if you think you want to use the method elsewhere without the instance of the object itself, use the static keyword which will result in better performance of your code. In this case, the reason the main method is static is because it is the method that runs first before thousands of classes in a big application. This means that you do not have an instance of an object yet, so it must be static. Note that many of these keywords apply to classes, methods and even variables interchangeably, providing similar meaning in each of the cases.

To print out “Hello” call the System.out.println() method inside your main with the hello string passed in as a parameter:

package hello;

public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello");
  }
}

Click on the green run project button and you should have a successful output “Hello”.

Consider an alternative where we call a static method inside the class

package hello;

public class Hello {
  public static void main(String[] args) {
    helloMethod(); // calling the method declared below
  }

  static void helloMethod() {
    System.out.println("Hello");
  }
}

Now that we learned a bit of Java, let’s discover some of the command line tools to do simple manual compilation. Jar files talk about the structure of Java projects and develop an idea how you might end up using the command line versus the IDE and just for the sake of understanding things better. Most of the IDE’s come with a built in command line tool for a reason. If you do not have the java path variable set up (windows machines) follow these steps to proceed. Also, if on windows, consider using a cmd tool such as Git Bash or other unix type cmd’s for windows.

Open the shell and change directory to your Java project. To do that quickly, right click on the project in your IDE, go to properties, sources to see the full path.

Here is how it might look:

cd "C:\Users\USERNAME\Documents\NetBeansProjects\Hello"

You should see the .java file in this directory. To compile run the following command

javac Hello.java

Let’s create a jar file from the class file we have. Jars are are basically archived java projects. Run the following command:

jar -cf hello.jar Hello.class

This will make a jar file in the current directory. The options -cf stand for create and specify file name. Run jar -h to see all available options.

To have a better touch and feel what can be done with the command line, let’s change our code and pass in some arguments. We’ll write a simple loop to iterate through our array of arguments that will be printed after “Hello”:

package hello;

public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello");
    for(int i=0; i<args.length; i++) {
      System.out.println(args[i]);
    }
  }
}

Compile the program and run

java Hello arg1 arg2 arg3

Make sure you are in the directory where your class file is. If you are not use the -cp (classpath option):

java -cp <path> Hello <args>

Enough of the basics. Dive in headfirst and create your first RESTful Services. Follow the articles written here: Creating a RESTful service with Java and Jersey with Netbeans, JPA Using NetBeans and MySQL, and to solidify the knowledge and use both read Combining JPA and JAX-RS to create RESTful service.


tags: java basics java RESTful services