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.
Join the DZone community and get the full member experience.Join For Free
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.
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.
In the project, I created a single class that simply writes to the console “Hello Universe.” when run.
Running the project now will output the message “Hello Universe.”
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.
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.
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.
Now you need to transfer the JAR file. Right-click on the file and select upload.
Your JAR file should be transferred to the Pi.
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.
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:
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
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!
Published at DZone with permission of Ken Fogel, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Harnessing the Power of Integration Testing
Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
Auditing Tools for Kubernetes
Execution Type Models in Node.js