Java Hello World! (Setting Up Your Java Development Environment)

If you’ve been following along with my earlier posts, so far you’ve installed Linux Mint, learned about the Django framework, learned how to use git, and hosted a Django Hello World! web app on Heroku. That’s quite a lot! In a later series I’ll be covering how to enhance your Django web app.

For now, though, I’m breaking from Django to show you how to set up and run a Hello World! environment using a few other languages, starting with Java, using Jetbrains’ IntelliJ IDEA!

 

On to the Tutorial!

 

java_and_intellij_logos

In this post we will cover:

  1. Installing the JDK
  2. Installing IntelliJ IDEA
  3. Setting up our first Java project
  4. Creating a Java class file
  5. Running a Hello World! program to output to the console

As always, please contact me or leave a comment if you have questions or would like further clarification!

 

Step 1: Install the JDK (Java Development Kit)

 

What is the JDK?

JDK stands for Java Development Kit. If you’ve run a Java application (in a web browser, for example), then you’ve probably experienced the JRE (Java Runtime Environment). The JDK is one step beyond the JRE, and actually includes the JRE, plus the tools and libraries needed to compile and run Java programs.

 

Installing the JDK Via Tarball

Go to Java’s JDK download page.

Accept the agreement and choose the correct tar.gz file for your machine. A tar.gz file is known as a compressed tarball, which is similar to a zip file. While a zip file is a compressed bundle of files, a .tar by itself simply bundles files together, while the .gz provides compression.

java-download-page

 

Figure out where you want Java installed

You can install the JDK wherever you like; there’s nothing stopping you from untarring the file right there in the Downloads directory. You’ll be able to point your IDE to the proper directory and run a Java program just fine. However, I’m a big fan of following common practices, and the common location to use for Java files is /usr/bin/. You will need to be a superuser (sudo) to move files to this location, so to make things simple, we’ll be using the command line.

java_where-to-install

 

Move, Untar, and Cleanup

Done!

Note: you can also take a look at Oracle’s official Java Linux  installation instructions.

 

Installing the JDK Via Repository

The above method is fine, but you’ll have to manually download and extract any future updates to the JDK. An alternative method is to add the repositories to your sources list. Then when you run apt-get update in the future, your JDK will automatically update.

Activate super user mode

Be sure to exit superuser mode as soon as these steps are completed.

 

Add the repositories to your source

 

Update apt-get and install Java

The install step will take several minutes.

jdk_jre_installed

Done!

 

Step 2: Choose an IDE (Integrated Development Environment)

In this demo, I’ll be installing and using Netbeans’ IntelliJ IDE. You can see IntelliJ’s documentation here. IntelliJ is great for learning Java since the the educational version is free.

 

Installing the IntelliJ IDEA

Navigate to Jetbrains’ IntelliJ for Linux download page, and download and install the tarball, just as we did with the JDK above, with the added step of executing the shell script. For more detailed instructions, you can reference these tarball installation steps.

Note: /opt/ is a common location for user applications.

Follow the prompts to customize the installation if you would like, otherwise just skip all and set defaults.

 

Add a Desktop Launcher

On the welcome screen, you can select Configure from the bottom-right and choose ‘Create Desktop Entry’ from the dropdown to allow you to start IntelliJ from the application launcher. You many need to log out of your session and back in before it will appear.

new_project_intelliJ

 

NOTE: If you get stuck, there are detailed installation instructions inside the IntelliJ directory, named something similar to idea-IC-143.1821.5.txt, which you can read with:

 

Step 3: Create a New Java Project

From the Welcome Screen, select ‘Create New Project’, then on the New Project screen, assign the SDK by clicking the New… button in the top right of the screen and selecting the JDK option.

java_select_JDK

 

IntelliJ should find the correct location of the JDKs. Select /usr/lib/jvm/java-8-oracle and click OK, then Next in the New Project window.

Type a project name, and hit Finish. The project will be created and the IDE will load.

java-project-name

 

Step 4: Create Your First Class File

Double-click on the project name in the header bar, beneath the menu, to expand the project menu.

java-project-panel

 

Expand the HelloWorld project, then right-click on the src folder and choose New > Java Class.

java-new-class

 

Give your new class a name and hit OK.

java-new-class-2

 

Step 5: Create the Main Method, Add Your Code, and Run It!

Java looks for the main method at run time, so we’ll need to create it. Without this method, the program won’t run. IntelliJ provides a handy shortcut that will create this method for us. Epitome of laziness? Maybe. But I like it.

Type psvm and then hit Tab. (psvm = public static void main).

java-psvm

java-psvm-created

 

Presto! Code shortcuts like this are just one great example of how the IDE makes things a little easier.

Next, let’s add our Hello World! code!

To print a line to the console, we use:

To run your code, right-click on the HelloWorld.java class file in the project tree, and choose “Run ‘HelloWorld.main()'”. Notice how the IDE detects that this file contains our main() method.

java-run-helloworld

 

The IDE will ‘make’ your project, compile the code, output ‘Hello World!’ to the console at the bottom of your IDE, and exit, since that’s all we asked it to do!

java-helloworld-console-output

 

Congratulations! You’ve configured your Java development environment and completed your first Hello World! project!

In future installations of my Java series, I’ll be building a Zork clone text adventure game (which hopefully will be a bit more exciting!) Please sign up for my  newsletter to receive updates about these posts!

(Posts) Sign Up for my Newsletter!
Sending

Matthew Odle

Indie dev, autodidactic polyglot, lover of programming, dad, musician, writer, mentor

Join the Discussion