最近发现树莓派支持Java语言了,Pi4J项目提供了树莓派常用资源的接口,按照官方的教程安装,可以在树莓派上运行Java项目。最近几天尝试安装体验了一番,这里贴上大概步骤。
建议在Windows上用Eclipse编写程序(需要手动导入必要的包),使用Eclipse编写起来程序就很流畅啦!然后可以使用WinSCP上传到树莓派的工作目录中,使用putty在树莓上编译和运行Java程序。官网介绍的很详细,还提供了很多Example可供学习。
树莓派联网后,在终端:
curl -s get.pi4j.com | sudo bash
This method will download and launch an installation script that perform the following steps:
- adds the Pi4J APT repository to the local APT repositories
- downloads and installs the Pi4J GPG public key for signature validation
- invokes the 'apt-get update' command on the Pi4J APT repository to update the local package database
- invokes the 'apt-get install pi4j' command to perform the download and installation
/opt/pi4j/lib
/opt/pi4j/examples
注意:在编译程序时,需要包含Pi4J的库文件路径:
javac -classpath .:classes:/opt/pi4j/lib/'*' ...
运行程序命令为:
sudo java -classpath .:classes:/opt/pi4j/lib/'*' ...
PS:编码问题,如果.java文件中包含汉字注释等,需要一下编译和运行命令:
javac -classpath .:classes:/opt/pi4j/lib/'*' -encoding gbk ...
sudo java -classpath .:classes:/opt/pi4j/lib/'*' -Dfile.encoding="GBK..."
官网上有详细的教程,后续尝试写写小程序来玩。向Pi4J项目致敬!