How to Install the Java Development Kit on a Raspberry Pi

The Java programming language was first released back in 1995, and since then it has grown to become a giant among giants. According to Oracle, there are more than 9 million Java developers worldwide. And because of Android, there are millions of smartphones which run Java, while simultaneously every Blu-ray player in the world ships with Java. Last, but not least, Java is available for the Raspberry Pi!

To install Java 8 on your Pi use the following command:

To check that everything installed OK, type:

java-version

The output shows that Java 1.8.0 has been installed. 1.8 means Java 8. The initial releases of Java were all “point” releases: 1.0, 1.1, 1.2, 1.3, and 1.4. But in 2004 when Java 1.5 was released, it was marketed as Java 5; however, 1.5 still remained the internal version number. So 1.5 is Java 5, 1.6 is Java 6, 1.7 is Java 7, and 1.8 is Java 8.

Now to write a simple Java program. Create a file called “Hello.java” using nano:

The capital “H” is important, so be sure you type “Hello.java” and not “hello.java”. Copy and paste the following code into the file:

In Java, the public class name must be the same as the file name. This makes it easier to find classes when you have big programs. If you need to find the class “TheNetworkListener,” then it will be in the file “TheNetworkListener.java,” and so on.

To compile the program type:

If the compilation was successful then there will be no output. But if the program has errors, then they will be displayed.

To run the program type:

The output will be the string “Hello Make Tech Easier”

java-hello

Congratulations, you have now written and run your first Java program on a Raspberry Pi.

You can also write GUI programs with Java. There are integrated development environments (IDE) like NetBeans, which allow you to design Java desktop applications graphically. But it is also possible to write GUI programs using just a text editor.

Create a file called “HelloDialog.java” with nano:

Copy and paste the following code:

Make sure you have the Raspberry Pi desktop running (using “startx” if necessary). Compile the program using “javac HelloDialog.java” and run it using “java HelloDialog“.

java-hello-gui

The program uses Java’s GUI widget toolkit called Swing. All it does is show a message dialog box. However, more complex programs can be written using Swing. Create a file called “SimpleSwing.java” and edit it with nano. Copy and paste the following code into the file.

The program is quite simple; however, it does show that you can create a window with a title. The window can be resized and closed, just as you would expect. This is the building block for more complex applications.

java-simple-swing

There are lots of tutorials online about the Java programming language. Here is a small selection:

If you have any problems with the examples given above or with getting Java to work on the Raspberry Pi, then please feel free to leave a comment below and we will see if we can help.

9 comments

  1. Hi,
    I’m getting this error:
    pi@HS-RASPI ~ $ sudo apt-get install oracle-java8-jdk
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    oracle-java8-jdk is already the newest version.
    The following packages were automatically installed and are no longer required:
    ca-certificates-java icedtea-6-jre-jamvm libnspr4 libnss3 libnss3-1d openjdk-6-jre-headless openjdk-6-jre-lib tzdata-java
    Use ‘apt-get autoremove’ to remove them.
    0 upgraded, 0 newly installed, 0 to remove and 13 not upgraded.
    1 not fully installed or removed.
    After this operation, 0 B of additional disk space will be used.
    Do you want to continue [Y/n]? y
    Setting up oracle-java8-jdk (8) …
    update-alternatives: error: /var/lib/dpkg/alternatives/java corrupt: line not terminated while trying to read status
    dpkg: error processing oracle-java8-jdk (–configure):
    subprocess installed post-installation script returned error exit status 2
    Errors were encountered while processing:
    oracle-java8-jdk
    E: Sub-process /usr/bin/dpkg returned an error code (1)

    Can you help?
    Thanks
    Matz

    • try sudo apt-get upgrade
      then install it using the following command
      apt-get install oracle-java8-jdk

  2. Hello, I was trying to run this program, but when i exit the nano to compile everything it says there is one error and it shows a carrot symbol under the j in java and my first impression was capitalization, but it did not work when I capitalized it. When i go to run it it would display an error message. Please help, if you would like me to give you greater detail, I will do so if someone responds.

      • Thank you very much for responding so quickly, I solved my problem: Two letters at the start of a word had to be capitalized. Thanks anyways, have a wonderful day.

  3. I’m wondering if anyone has managed to make the I/O pins work through Java. I’m not completely abject to learning a new language, but have many years of development experience in Java, so it’s my comfort zone. Can you provide an example of I/O pin control or point me to it?

Comments are closed.

Sponsored Stories