My First Java Program

Step 1. Write a simple Java program

Create a new text file, called Intro.java and place it somewhere on your computer, like your Documents folder.

Next, add the code from Listing 1, which is a very simple Java program.

Listing 1. Intro.java

public class Intro {

    public static void main(String[] args) {
        System.out.println("Welcome to the JDK!");
    }

}

Step 2. Compile with the JDK

Next, use the JDK compiler to turn your text file into an executable program. Compiled code in Java is known as bytecode, and carries the .class extension.

You’ll use the javac command, which stands for Java compiler. Type the full path to the command into your command shell, and pass the Intro.java file as a command. On my system, that looks like Listing 2.

Listing 2. Compile with the JDK

"C:\Program Files\Java\jdk-10.0.1\bin\javac.exe" Intro.java

That should result in a successful compile. The javac will not respond with a success message; it will just output the new file. Any errors will result in console output.

Step 3. Run the .class file

You should now see the Intro.class file in the same directory as Intro.java.

You can run it by typing: java Intro, which will result in Listing 3. Note that you don’t include the .class when typing this command.

Listing 3. Running Intro.class

C:\Users\mtyson\Documents>java Intro
Welcome to the JDK!

The jar command

The javac is the star of the JDK, but the /bin directory contains other tools you will need. Probably the most prominent after javac is the jar tool.

.jar file is a packaged set of Java classes. Once the compiler has created the .class files, the developer can put them together in a .jar, which compresses and structures them in a predictable fashion.

Let’s convert Intro.class to a jar file.

Navigate back to the directory where you placed your Intro.java, and type the command you see in Listing 4.

Listing 4. Create a JAR file

C:\Users\mtyson\Documents>"c:\Program Files\Java\jdk-10.0.1\bin\jar.exe" --create --file intro.jar Intro.class

Executing the jar

Now you’ll see an intro.jar file in the directory. You can make use of the .jarby adding it to your classpath and executing the program inside, as shown here:

java -cp intro.jar Intro

The -cp switch tells Java to add the jar to the classpath. A .jar file is overkill for this tiny program, but they’re indispensable as programs grow in size and rely on third-party packages.

The JDK in your IDE

Looking back to the JDK download page, you may have noticed the option to download the JDK with the Netbeans IDE. An IDE, or integrated development environment, is software that provides a cohesive set of tools for developing applications. Think of an IDE as a visual operating system with a set of tools, like a file browser and text editor, with additional capabilities specific to development, like code completion and formatting.

In Java development, one of the key things the IDE does is manage compilation for you. That is, the IDE automatically runs the compile process in the background so you don’t have to continually do it yourself. An IDE also provides play-by-play feedback as you go, catching coding errors on the fly.

Several solid IDEs exist for Java. You’ve seen how the JDK works on the command-line, so now Let’s take a quick look at how it works in the IntelliJ IDE.

Now lets install our downloaded tools(IntelliJ and JDK) if you haven’t downloaded,then kindly go back to the tools page to download them.

Leave a comment