Creating a Simple Raspberry Pi Application Using Java (HelloRaspberryPi)
Objective
Now that youve established SSH access to your Raspberry Pi, the goal is to:
- Create a basic Java program named `HelloRaspberryPi`
- Compile it using `javac`
- Run it using the `java` command
This helps verify that Java is installed and working, and that your SSH-based remote development
environment is ready.
What You'll Create
A simple Java program that outputs:
"Hello Raspberry Pi"
Step-by-Step Guide
Step 1: Log in to Raspberry Pi via SSH
Use terminal or PuTTY:
ssh
[email protected]or
ssh
[email protected]Step 2: Create Java Source File Remotely
Use echo to create the file:
echo "class HelloRaspberryPi {
> public static void main(String[] args) {
> System.out.println(\"Hello Raspberry Pi\");
>}
> }" > HelloRaspberryPi.java
This creates HelloRaspberryPi.java in your current directory.
Step 3: Compile the Java Code
Compile using:
javac HelloRaspberryPi.java
If Java is not installed, use:
sudo apt-get install default-jdk
Step 4: Run the Java Program
Run the program:
java HelloRaspberryPi
Expected Output:
Hello Raspberry Pi
Concept Recap Table
| Step | Command/Concept | Purpose |
|------|------------------|---------|
| Create file | `echo ...` | Create Java source file |
| Compile Java | `javac` | Compile to .class file |
| Run Java App | `java` | Execute the class file |
| Escape Quotes | `\"...\"` | Proper syntax in echo |
Real-World Importance
This process helps you:
- Learn remote coding techniques
- Run applications headlessly
- Prepare for Java-based IoT projects on Raspberry Pi