Raspberry Pi – Hello World with Eclipse and Java

Introduction

While looking for tutorials about programming the Raspberry Pi, i noticed that most examples are based either on Python or C/C++. However, my preferred programming language is Java and there was suprisingly little to be found in this direction.

So in order to improve the situation a bit, i decided to capture my experiences in a couple of blog posts. This and all follow-up posts are targeted to those who want to use Java as programming language of choice for their Raspberry Pi projects.

I want to point out that i expect some basic knowledge in Java software development, Eclipse IDE, Maven and other related topics from the reader. However, i will also provide all examples as downloadable projects via Git; so it will be easy even for less experienced readers to experiment with the software and to use it as starting point for their own projects.

Git Repository

The example project for this article is org.raspirecipes.helloworld available in the following Git repository:

The Development Environment

Before getting started with the development of Java applications for the Raspberry Pi, we have to set up a development environment.

While it is possible to run IDEs like Eclipse directly on the Raspberry Pi 3 to some extend, a better approach is to use of a more powerful PC, notebook or workstation for this task. Doing this, however, leaves us with some challenges: the IDE used to implement and compile the programs will run on the workstation, but the programs themselves should be deployed, executed and debugged on the RaspberryPi.

Luckily, with some small amount of work we can set up a development environment that enables us to automatically deploy, execute and remote-debug a Java application on the Raspberry Pi.

Here are the tools that i used to accomplish this:

Preparing the RaspberryPi

Here i assume the the Raspberry Pi has already been prepared with a Raspian and is connected to the same network as the workstation.

Static IP Address

The first step is to provide the Raspi with a static IP address. There are two ways to do it:

Provide the IP via DHCP Server (Preferred)

Many home routers allow to set static IP addresses for connected devices. On my FritzBox, this could be set on the following page:

StaticIP_FritzBoxI prefer this method as it is the most flexible. When i move my Raspi to a different WiFi network, it will still connect to the local router and receive it’s IP address and network configuration dynamically.

Static IP via DHCPD

Another way is to change the DHCPCD configuration of the Raspi. In the following example, we set the address for the WiFi interface wlan0.

Please note that the actual values are just examples and have to be adjusted to match the IP network that Raspi is connected to.

Enable SSH Server

The second step is to enable the SSH server on the Raspi. This can easily be done with raspi-config:

Then navigate to 9 Advanced Options -> A4 SSH and enable the SSH server.

raspi-config_SSHFinally, if everything is set up correctly, it should be possible to connect via SSH from the workstation to the Raspi:

Remote Debugging with the Eclipse IDE

In the final steps, we will create a Hello-World Java project with the Eclipse IDE and automatically deploy and remote-debug it on the Raspi.

Create the Hello World Project

I opted to create a Java project using Maven right from the beginning. There are various reasons; here are my top 3:

  1. it will be easy to add additional libraries and their dependencies to the project
  2. Maven can easily be executed from the IDE, from command line and within continuous integration servers like Jenkins
  3. Maven by convention places the jar file containing the executable code in the /target subfolder of the project; this simplifies the remote deployment via a script

Creating the project is relatively easy:

  1. Start your Eclipse IDE (Maven integration M2Eclipse has to be installed)
  2. In the Package Explorer, right-click and select New -> Other… -> Maven Project
  3. Select “create a simple project (skip archetype selection)”
  4. Enter group-id, artifact-id and other project attributes
  5. Push “Finish” to create the project

new-maven-project-hello-worldAfterwards, create the HelloWorld.java class containing the main method.

eclipse-ide-hello-world-classFinally, edit the pom.xml of the project and add a few lines so Maven knows that the HelloWorld.class will be the entry point of the program:

Ant Script for Deployment and Program Start

I am using an Ant script to

  • deploy the program to the Raspi using SCP
  • start the program it debug mode with suspend=y, making it wait until the IDE connects

Create the remotedebug.xml file in the project root folder and copy the following content. Please note that the first four properties have to be adjusted!

Eclipse Remote Debug Launch Configuration

Finally, a debug launch configuration has to be set up in the Eclipse IDE.

In the Main Menu, go to Run -> Debug Configurations… -> Remote Java Applications.

Create a new launch configuration there and

  • select the remotehelloworld project
  • select Connection Type “Socket Attach”
  • enter the Raspberry Pis IP address as host
  • enter the Java VMs debug port set in the Ant script (port 4000)

Perform the Remote Debugging

To perform the remote debugging, do the following:

  1. build the project with Maven (right-click -> Run As -> Maven install)
  2. verify that the build was successful and that the jar file has been placed into the /target folder of the project
  3. add a breakpoint to the HelloWorld.java class (e.g. in the system.out line)
  4. execute the remotedebug.xml Ant script
  5. observe the console; wait for the line [sshexec] Listening for transport dt_socket at address: 4000
  6. Start the remotehelloworld_debug launch configuration

eclipse-ide-hello-world-debug-configCongratulations!

If everything works as expected, the Eclipse IDE will switch to the debug perspective. It will show the Hello-World program running on the Raspberry Pi. The programs main thread will be suspended on the breakpoint in the HelloWorld class.

eclipse-ide-hello-world-remote-debugging

One comment

Add Comment

Required fields are marked *. Your email address will not be published.