Java & Raspberry Pi Tutorial

Throughout the process of creating my Java tutorial series I’ve had numerous requests for a large project tutorial that would demonstrate many of the concepts that I’ve taught. So I came up with an idea … what better way to apply new Java skills than to control hardware using software? We are going to blink LEDs, control servos, use switches, and play around with a multitude of sensors all controlled with programs that we write using Java ME Embedded.

The ME in Java ME is short for Micro Edition and if you have been following my Java tutorial series then you have been using Java SE which is short for Standard Edition. Some of the language features of Java SE are not available in ME and vice-versa. As some of you know I have been busy writing apps for a while, but now that I have some more free time on my hands, I will split my spare time between creating SE and ME tutorials.

With that being said, one thing that you will need in order to follow along with my ME tutorials is a Raspberry Pi. I recommend that you have at the very least, the Raspberry Pi 3 model B - older versions may or may not allow ME to be installed. The Pi 3 model B comes with Java SE 8 pre-installed and is compatible with Java ME Embedded 8.3+. I also have a very high level of certainty that future versions of the Raspberry Pi will maintain backwards compatibility with the way the I/O pins are configured. I will go over installing and configuring Java ME Embedded in my next tutorial.

The purpose of these videos is to learn Java and explore IoT technology (Internet of Things). However, I am going to spend a little time talking about how I have configured my Raspberry Pi for those of you that are new to the device. The Raspberry Pi community is quite extensive and you will be able to find answers for just about everything. See video. Click here for Remote Desktop connection error fix.



Open the Terminal window and type in the following commands.

pi@raspberrypi:~ $ mkdir Java
pi@raspberrypi:~ $ cd Java
pi@raspberrypi:~/Java $ mkdir Hello
pi@raspberrypi:~/Java $ cd Hello
pi@raspberrypi:~/Java/Hello $ leafpad Hello.java

Copy and Paste, or type the following code into leafpad and be sure to save the file when you are done.


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

Now switch back to the Terminal and type in javac HelloPi.java and press Enter.
Now type in java HelloPi and press Enter.


pi@raspberrypi:~/Java/Hello $ Javac HelloPi.java
pi@raspberrypi:~/Java/Hello $ Java HelloPi
Hello Pi!!!


Final thoughts

If you have been following my Java tutorial series, then you know I am a huge fan of learing Java via the command prompt. Don't get me wrong, I love my IDE's and use them all the time, but they do not provide the sort of "low-level" nuts and bolts learning required to truly understand how things work. Raspberry Pi has a couple of pre-installed Java IDE's that are quite primitive compared to Eclipse, but of course RPi is quite young. There is a GPIO project out there, but I am going to teach you how to control I/O using Java Classes. Stay tuned for my next tutorial where I will show you how to install and configure Java ME Embedded and run our first program.


Pi Tutorials