In this post we will show how to set up Eclipse to deploy a Java application to a Raspberry Pi located on the local network. We have adapted from this article to reflect fixes to common errors.
Set up Raspberry Pi
- Insure that your RPi is connected to your local network and that it has been assigned a permanent IP address. You can do this via your router settings or by modifying the /etc/network/interfaces file in your RPi.
- Insure that you RPi has SSH enabled. Goto Preferences—>Raspberry Pi Configuration—>Interfacing Options and select SSH.
Set up eclipse
- Install Maven if you have not already. If you do not know what Maven is, take some time to learn before jumping to remote programming!
- Enable the Consol View:
Window-->Show View-->Consol
- Enable the Ant View:
Window-->Show View-->Other-->Ant-->Ant
Set up Maven project
If you have not already set up a Maven project, follow instruction (2) of this blog.
Modify pom.xml
Edit the build section of the pom.xml to point to your main class. In the pom.xml below this is <mainClass>pi.hello.App</mainClass>
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>pi</groupId> <artifactId>hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hello</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- Add the maven pluggin to build --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>pi.hello.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> |
Create build.xml file
- Right click in the project explorer and click
New-->Other-->XML-->XML File
- Select project root folder and name file ‘build.xml’
Paste the following into the build.xml
file, insuring that the username, password and IP address of your pi is correct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<?xml version="1.0" encoding="UTF-8"?> <project name="hello" default="remote-run" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant"> <!-- Setup RASPBERRY PI properties --> <property name="raspberrypi" value="192.168.1.167" /> <property name="raspberryfolder" value="~" /> <property name="username" value="pi" /> <property name="password" value="raspberry" /> <!-- <path id="maven-ant-tasks.classpath" path="${ant.libs.dir}/maven-ant-tasks-2.1.3.jar" /> --> <path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.3.jar" /> <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath" /> <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath" /> <!-- Add maven install target to be run before deploy --> <target name="maven-install"> <artifact:mvn pom="pom.xml"> <arg value="install"/> </artifact:mvn> </target> <!-- Locate the prokect jar and transfer via scp to RASPBERRY PI --> <target name="transfer" depends="maven-install"> <first id="jars"> <fileset dir="target" includes="**/*-SNAPSHOT-jar-with-dependencies.jar" /> <!--<fileset dir="target" includes="**/*.jar" /> --> </first> <pathconvert pathsep="," property="jar.path" refid="jars" /> <basename file="${jar.path}" property="jar.filename" /> <echo>">>> Found application ${jar.path}"</echo> <echo>">>> Copying application to ${raspberrypi}:${raspberryfolder}/${jar.filename}"</echo> <scp localfile="${jar.path}" todir="${username}:${password}@${raspberrypi}:${raspberryfolder}" trust="true" /> </target> <!-- Run java --> <target name="remote-run" depends="transfer"> <echo>">>> Starting ${raspberrypi}:${raspberryfolder}/${jar.filename}"</echo> <sshexec host="${raspberrypi}" username="${username}" password="${password}" trust="true" failonerror="true" usepty="true" command="java -jar ${jar.filename}" /> </target> <!-- Run java in debug mode and keep waiting for execution --> <target name="remote-debug" depends="transfer"> <echo>">>> Starting ${raspberrypi}:${raspberryfolder}/${jar.filename} in debug mode"</echo> <sshexec host="${raspberrypi}" username="${username}" password="${password}" trust="true" failonerror="true" usepty="true" command="java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=y -jar ${jar.filename}" /> </target> </project> |
Run the ant file
Right click on build.xml
and click on Run As-->Ant Build
. If everything is set up and working, your output should look like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
Buildfile: ~/workspaces/RaspberryPi/hello/build.xml maven-install: [artifact:mvn] [INFO] Scanning for projects... [artifact:mvn] [INFO] ------------------------------------------------------------------------ [artifact:mvn] [INFO] Building hello [artifact:mvn] [INFO] task-segment: [install] [artifact:mvn] [INFO] ------------------------------------------------------------------------ [artifact:mvn] [INFO] [resources:resources] [artifact:mvn] [INFO] Using 'UTF-8' encoding to copy filtered resources. [artifact:mvn] [INFO] skip non existing resourceDirectory ~/workspaces/RaspberryPi/hello/src/main/resources [artifact:mvn] [INFO] [compiler:compile] [artifact:mvn] [INFO] Nothing to compile - all classes are up to date [artifact:mvn] [INFO] [resources:testResources] [artifact:mvn] [INFO] Using 'UTF-8' encoding to copy filtered resources. [artifact:mvn] [INFO] skip non existing resourceDirectory ~/workspaces/RaspberryPi/hello/src/test/resources [artifact:mvn] [INFO] [compiler:testCompile] [artifact:mvn] [INFO] Nothing to compile - all classes are up to date [artifact:mvn] [INFO] [surefire:test] [artifact:mvn] [INFO] Surefire report directory: ~/workspaces/RaspberryPi/hello/target/surefire-reports [artifact:mvn] ------------------------------------------------------- [artifact:mvn] T E S T S [artifact:mvn] ------------------------------------------------------- [artifact:mvn] Running pi.hello.AppTest [artifact:mvn] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.017 sec [artifact:mvn] Results : [artifact:mvn] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [artifact:mvn] [INFO] [jar:jar] [artifact:mvn] [INFO] [assembly:single {execution: make-assembly}] [artifact:mvn] [INFO] Processing DependencySet (output=) [artifact:mvn] [INFO] Building jar: ~/workspaces/RaspberryPi/hello/target/hello-0.0.1-SNAPSHOT-jar-with-dependencies.jar [artifact:mvn] [INFO] [install:install] [artifact:mvn] [INFO] Installing ~/workspaces/RaspberryPi/hello/target/hello-0.0.1-SNAPSHOT.jar to ~/.m2/repository/pi/hello/0.0.1-SNAPSHOT/hello-0.0.1-SNAPSHOT.jar [artifact:mvn] [INFO] Installing ~/workspaces/RaspberryPi/hello/target/hello-0.0.1-SNAPSHOT-jar-with-dependencies.jar to ~/.m2/repository/pi/hello/0.0.1-SNAPSHOT/hello-0.0.1-SNAPSHOT-jar-with-dependencies.jar [artifact:mvn] [INFO] ------------------------------------------------------------------------ [artifact:mvn] [INFO] BUILD SUCCESSFUL [artifact:mvn] [INFO] ------------------------------------------------------------------------ [artifact:mvn] [INFO] Total time: 3 seconds [artifact:mvn] [INFO] Finished at: Wed May 08 13:07:14 MDT 2019 [artifact:mvn] [INFO] Final Memory: 34M/702M [artifact:mvn] [INFO] ------------------------------------------------------------------------ transfer: [echo] ">>> Found application ~/workspaces/RaspberryPi/hello/target/hello-0.0.1-SNAPSHOT-jar-with-dependencies.jar" [echo] ">>> Copying application to 192.168.1.167:~/hello-0.0.1-SNAPSHOT-jar-with-dependencies.jar" [scp] Connecting to 192.168.1.167:22 [scp] done. remote-run: [echo] ">>> Starting 192.168.1.167:~/hello-0.0.1-SNAPSHOT-jar-with-dependencies.jar" [sshexec] Connecting to 192.168.1.167:22 [sshexec] cmd : java -jar hello-0.0.1-SNAPSHOT-jar-with-dependencies.jar [sshexec] Hello World! from raspberrypi BUILD SUCCESSFUL Total time: 6 seconds |
Issues and Resolutions
Maven Build Issue
If you get the following error:
CoreException: Could not get the value for parameter compilerId for plugin execution default-compile: PluginResolutionException: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one of its dependencies could not be resolved: Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact org.codehaus.plexus:plexus-utils:jar:1.1 has not been downloaded from it before.
Resolve by deleting the Maven folder org.apache.maven.plugins:maven-compiler-plugin:3.1
This is located in the folder ~/.m2/repository/org/apache/maven/plugins/maven-compiler-plugin:/3.1
If you do not see the hidden folder .m2 on your Mac, enable viewing of hidden folders.
Ant Dependency Issues
If you get the following error…
1 2 3 4 5 6 7 8 9 10 11 12 13 |
BUILD FAILED /Users/alexnugent/workspaces/RaspberryPi/hello/build.xml:21: Problem: failed to create task or type antlib:org.apache.maven.artifact.ant:mvn Cause: The name is undefined. Action: Check the spelling. Action: Check that any custom tasks/types have been declared. Action: Check that any <presetdef>/<macrodef> declarations have taken place. No types or tasks have been defined in this namespace yet This appears to be an antlib declaration. Action: Check that the implementing library exists in one of: -/Users/alexnugent/.p2/pool/plugins/org.apache.ant_1.10.5.v20180808-0324/lib -/Users/alexnugent/.ant/lib -a directory added on the command line with the -lib argument |
create a lib folder, and put this jar in it: https://search.maven.org/artifact/org.apache.maven/maven-ant-tasks/2.1.3/jar
If you get this error…
1 2 3 4 5 6 7 8 |
Cause: Could not load a dependent class com/jcraft/jsch/Logger It is not enough to have Ant's optional JARs you need the JAR files that the optional tasks depend upon. Ant's optional task dependencies are listed in the manual. Action: Determine what extra JAR files are needed, and place them in one of: -/Users/alexnugent/.p2/pool/plugins/org.apache.ant_1.10.5.v20180808-0324/lib -/Users/alexnugent/.ant/lib -a directory added on the command line with the -lib argument |
Download the dependent jar from here: https://sourceforge.net/projects/jsch/
And place into your project lib folder. If you do not have a lib folder, create one in the project root directory.
Go into the Eclipse Preferences-->Ant/Runtime entry-->'Classpath' tab
and use the button Add External Jars
to add your jsch.jar
to Ant’s classpath.
Are you interested in the cutting edge of AI processors? Read how to build the Ex-Machina Wetware Brain and learn about the Knowm Technology Stack.
3 Comments