Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Running Maven-Based Projects on a Remote Raspberry Pi Using NetBeans (Part 1)

DZone's Guide to

Running Maven-Based Projects on a Remote Raspberry Pi Using NetBeans (Part 1)

In the first half of a two-part series, see one way of running Java projects remotely on your Raspberry Pi using NetBeans.

· IoT Zone
Free Resource

Cisco IoT makes digital transformation a reality in factories, transportation, and utilities. Learn how to start integrating with Cisco DevNet.

In the course of writing a series of articles titled ‘IoT without the Breadboard’ I came across an issue that I felt could be fun to resolve. The issue is how to copy to and execute on a Raspberry Pi a compiled Java application. As a NetBeans Dream Team member, my IDE of choice is NetBeans.

If you are already a NetBeans user playing with Pi systems, you already know it can be done if you create a native NetBeans project. My issue derives from the fact that I use Maven rather than NetBeans or any other IDE’s internal build system.

NetBeans has the capability to use an internal ssh client to copy a file to a remote Linux based system, such as a Pi, execute the code, and display its output in the NetBeans console window. This really cool standard feature is not available if the project is using the Maven build system.

So why don’t I just ditch Maven? As a teacher, I believe that it is important for students to learn how to build projects independent from the IDE. When I teach Java, I want my students to submit code that is IDE agnostic. My Raspberry Pi project is personal, but I felt that it would be hypocritical of me if I chose not to use Maven when I rant and whine at programmers who don’t use it.

There are two solutions to getting code from a Windows PC to a Linux PC. The solutions should work with any Linux based system and any version of Windows.

Here is the first solution. The next solution will be in the second part of this series.

My real interest in coding but I realized I was going to have to be a DevOp to make this work. There are three tasks to accomplish. First I have to write and compile my Java code, and for this I use, of course, NetBeans. Secondly, I need to copy the jar, war or ear file to my Raspberry Pi. For this, I chose Filezilla.

The third task is to execute the Java program from the command line. Here I was excited to find a program called xrdp, which runs on the Pi and allows me to use Microsoft Remote Desktop to access the Raspbian OS desktop on my Windows machine. To install xrdp you need to open a terminal window on your Pi that I assume you have currently connected to a keyboard, mouse and display. Here is a good article on installing xrdp. From this point forward you should rarely need to connect a keyboard, mouse, and display to your Pi. Use Remote Desktop instead.

With NetBeans, I created a new Maven Java Application.

NBNewProject

When the project is created by NetBeans this is what the pom.xml file should look like. If you are using an older version of Java or NetBeans it may be slightly different.

Maven_pom

In the project, I created a single class that simply writes to the console “Hello Universe.” when run.

HelloCode

Running the project now will output the message “Hello Universe.”

HelloUniverse

Now you need to transfer the JAR file to the Raspberry Pi. Start up Filezilla. If you are unfamiliar with Filezilla then you can use other programs such as PuTTY or WinSCP. There are probably many others that will allow you perform a secure copy from Windows to a Linux system.

Using Filezilla, locate the folder ‘target’ in the MavenWinToPi project. In this folder, you should find a JAR file. If there is no JAR file, go back to NetBeans and perform a Clean and Build on the project.

Now connect to your Raspberry Pi. Here is the Filezilla display after locating the target folder and then configuring the connection. You will need to know the IP number of the Pi. I use my router’s control panel to see what internal (192.168.x.x) number it has been given.

filezilla

Take note of the file name of the JAR file, in my case its ‘MavenWinToPi-1.0-SNAPSHOT.jar’. Click on connect and you should see your home directory on the Pi.

If this is the first time that you are using this program, you will be asked to accept the unknown ssh key from the Pi. If the IP number of your Pi changes, you will also be asked to verify the key again.

hostkey

Check off the ‘Always trust . . .’ box otherwise you will have to approve the host key every time you connect. Assuming that all has gone well, you should see your Pi on the right side of the Filezilla window.

filezillaconnect


Now you need to transfer the JAR file. Right-click on the file and select upload.

upload

Your JAR file should be transferred to the Pi.

upload_success

 

Now it's time to execute the code. On Windows run the Remote Desktop Connection and configure it to connect to your Pi. You can let windows hold the user name and password or have it ask you for them for every connection. I let Remote Desktop hold my credentials.

rdp

By default, Remote Desktop will open full screen when it connects. I set the Display option to 1024 x 768 so that the Pi is running in a window I can move around. If all has gone well you should see:

xrdp_desktop

Open a terminal window on the Pi. You should now be able to execute your program at the command line by entering:

java -cp MavenWinToPi-1.0-SNAPSHOT.jar com.kenfogel.mavenwintopi.MavenWinToPi

execution

You should see Hello Universe.

This workflow for Java development for a Raspberry Pi works well and you don’t need to read any further. It is your only option if the program is interactive in the console or has a Swing or JavaFX GUI. If you are interested in automating this further so that everything happens in NetBeans, read the second article in this series.

This coming January 2017, Dawson College will host Montreal’s first NetBeans Day. Unlike large commercial conferences, NetBeans Day is a community organized event. It is loosely based around a central theme of NetBeans that supports a very wide range of languages, frameworks, and techniques. The event is free to attend (with refreshments) and targeted at professional software developers, students, and anyone with a general curiosity. Check it out!

Cisco is a software company. Surprised? Don’t be. Join DevNet to explore APIs, tools, and techniques that developers are using to add collaboration, IoT, security, network priority, and more!

Topics:
maven ,raspberry pi ,netbeans ,remote ,iot

Published at DZone with permission of Ken Fogel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}