从 java (jar) 中检测 raspberry pi 的理想方法
The ideal way to detect a raspberry pi from java (jar)
您好,我想知道检测 jar/java 程序是否在 raspberry pi 上 运行 的理想方法。
我为什么要那个?我想在 raspberry pi 中使用看门狗,但我也使用 windows 中的 java 程序,它不需要或不需要看门狗。
有没有办法像某些人检测操作系统一样检测树莓派上的 jar 是否 运行?
人们使用它的方式相同...
class ShowProperties {
public static void main(String[] args) {
System.getProperties().list(System.out);
}
}
您应该能够通过测试这些 Java 系统属性的值来了解您的 JVM 运行正在运行的操作系统的详细信息:
os.name
操作系统名称
os.arch
操作系统架构
os.version
操作系统版本
使用System.getProperty(name)
获取系统属性的值。
如果这不够精确,那么您需要求助于不可移植的解决方案,例如使用 System.exec(...)
到 运行 uname -1
或类似方法。
注意:您在系统上看到的 "raspberrypi" 实际上是默认主机名,并不是可靠的指标。 (它通常会被用户设置为其他东西。)
正如 Stephen C 所说,您可以使用 os.* 属性。
您可以从 Java 应用程序中 运行 uname -a,但这可能不是一个好主意,因为格式变化很大。我的 RPI2 运行ning OMC(媒体播放器发行版)我得到了你的代码:
os.arch=arm
os.name=Linux
os.version=4.4.6-3-osmc
和 uname -a 报告:
Linux osmc 4.4.6-3-osmc #1 SMP PREEMPT Fri Apr 1 20:55:03 UTC 2016 armv7l GNU/Linux
也可以看看PI4J项目。他们有一个 example 输出有关 PI 的信息,它是 运行ning。尝试查看代码,了解他们是如何做到的。
这是我的一位助手 类 的一些代码。注意 isPiUnix 是如何设置为 true 的。此技术依赖于 os-release 文件中的 PRETTY_NAME 或 NAME 值。进行必要的调整以检测您支持的系统的任何变化。请注意 isLinux 和 isPiLinux 都会 return 当 运行 在预期的 Pi 上时为真。您只需要测试 isPiLinux.
private static boolean isWindows = false;
private static boolean isLinux = false;
private static boolean isHpUnix = false;
private static boolean isPiUnix = false;
private static boolean isSolaris = false;
private static boolean isSunOS = false;
private static boolean archDataModel32 = false;
private static boolean archDataModel64 = false;
static {
final String os = System.getProperty("os.name").toLowerCase();
if (os.indexOf("windows") >= 0) {
isWindows = true;
}
if (os.indexOf("linux") >= 0) {
isLinux = true;
}
if (os.indexOf("hp-ux") >= 0) {
isHpUnix = true;
}
if (os.indexOf("hpux") >= 0) {
isHpUnix = true;
}
if (os.indexOf("solaris") >= 0) {
isSolaris = true;
}
if (os.indexOf("sunos") >= 0) {
isSunOS = true;
}
if (System.getProperty("sun.arch.data.model").equals("32")) {
archDataModel32 = true;
}
if (System.getProperty("sun.arch.data.model").equals("64")) {
archDataModel64 = true;
}
if (isLinux) {
final File file = new File("/etc", "os-release");
try (FileInputStream fis = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis))) {
String string;
while ((string = br.readLine()) != null) {
if (string.toLowerCase().contains("raspbian")) {
if (string.toLowerCase().contains("name")) {
isPiUnix = true;
break;
}
}
}
} catch (final Exception e) {
e.printStackTrace();
}
}
}
此处以 Java42 的回答为基础是一个完整的帮助程序 class。
Java 代码中的用法
if (Raspberry.isPi()) {
...
}
测试结果
javac Raspberry.java
java Raspberry
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
Raspberry.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
* Raspberry PI helper class
* @author wf
*
*/
public class Raspberry {
public static boolean debug = false;
/**
* check if this java vm runs on a raspberry PI
*
*
* @return true if this is running on a Raspbian Linux
*/
public static boolean isPi() {
String osRelease = osRelease();
return osRelease != null && osRelease.contains("Raspbian");
}
/**
* read the first line from the given file
*
* @param file
* @return the first line
*/
public static String readFirstLine(File file) {
String firstLine = null;
try {
if (file.canRead()) {
FileInputStream fis = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(fis));
firstLine = bufferedReader.readLine();
fis.close();
}
} catch (Throwable th) {
if (debug)
th.printStackTrace();
}
return firstLine;
}
/**
* get the operating System release
*
* @return the first line from /etc/os-release or null
*/
public static String osRelease() {
String os = System.getProperty("os.name");
if (os.startsWith("Linux")) {
final File osRelease = new File("/etc", "os-release");
return readFirstLine(osRelease);
}
return null;
}
/**
* entry point to call from command line
*/
public static void main(String args[]) {
if (isPi()) {
System.out.println(osRelease());
}
}
}
这个函数会检测是否是Raspberry Pi
private static StringBuilder StringBuilderResult;
private static boolean DetectRaspBPi() {
GetInfoByExecuteCommandLinux("cat /proc/device-tree/model", false);
return StringBuilderResult.toString().toLowerCase().contains("raspberry");
}
private static void GetInfoByExecuteCommandLinux(String command, boolean getList){
try {
Process pb = new ProcessBuilder("bash", "-c", command).start();
BufferedReader reader=new BufferedReader(
new InputStreamReader(pb.getInputStream())
);
String line;
if (getList){
ListStringBuilderResult = new ArrayList<>();
} else {
StringBuilderResult = new StringBuilder();
while((line = reader.readLine()) != null)
{
if (getList){
ListStringBuilderResult.add(line);
} else {
StringBuilderResult.append(line);
}
}
} catch (Exception e){
System.out.println(e.getMessage());
}
}
您好,我想知道检测 jar/java 程序是否在 raspberry pi 上 运行 的理想方法。
我为什么要那个?我想在 raspberry pi 中使用看门狗,但我也使用 windows 中的 java 程序,它不需要或不需要看门狗。
有没有办法像某些人检测操作系统一样检测树莓派上的 jar 是否 运行?
人们使用它的方式相同...
class ShowProperties {
public static void main(String[] args) {
System.getProperties().list(System.out);
}
}
您应该能够通过测试这些 Java 系统属性的值来了解您的 JVM 运行正在运行的操作系统的详细信息:
os.name
操作系统名称os.arch
操作系统架构os.version
操作系统版本
使用System.getProperty(name)
获取系统属性的值。
如果这不够精确,那么您需要求助于不可移植的解决方案,例如使用 System.exec(...)
到 运行 uname -1
或类似方法。
注意:您在系统上看到的 "raspberrypi" 实际上是默认主机名,并不是可靠的指标。 (它通常会被用户设置为其他东西。)
正如 Stephen C 所说,您可以使用 os.* 属性。
您可以从 Java 应用程序中 运行 uname -a,但这可能不是一个好主意,因为格式变化很大。我的 RPI2 运行ning OMC(媒体播放器发行版)我得到了你的代码:
os.arch=arm
os.name=Linux
os.version=4.4.6-3-osmc
和 uname -a 报告:
Linux osmc 4.4.6-3-osmc #1 SMP PREEMPT Fri Apr 1 20:55:03 UTC 2016 armv7l GNU/Linux
也可以看看PI4J项目。他们有一个 example 输出有关 PI 的信息,它是 运行ning。尝试查看代码,了解他们是如何做到的。
这是我的一位助手 类 的一些代码。注意 isPiUnix 是如何设置为 true 的。此技术依赖于 os-release 文件中的 PRETTY_NAME 或 NAME 值。进行必要的调整以检测您支持的系统的任何变化。请注意 isLinux 和 isPiLinux 都会 return 当 运行 在预期的 Pi 上时为真。您只需要测试 isPiLinux.
private static boolean isWindows = false;
private static boolean isLinux = false;
private static boolean isHpUnix = false;
private static boolean isPiUnix = false;
private static boolean isSolaris = false;
private static boolean isSunOS = false;
private static boolean archDataModel32 = false;
private static boolean archDataModel64 = false;
static {
final String os = System.getProperty("os.name").toLowerCase();
if (os.indexOf("windows") >= 0) {
isWindows = true;
}
if (os.indexOf("linux") >= 0) {
isLinux = true;
}
if (os.indexOf("hp-ux") >= 0) {
isHpUnix = true;
}
if (os.indexOf("hpux") >= 0) {
isHpUnix = true;
}
if (os.indexOf("solaris") >= 0) {
isSolaris = true;
}
if (os.indexOf("sunos") >= 0) {
isSunOS = true;
}
if (System.getProperty("sun.arch.data.model").equals("32")) {
archDataModel32 = true;
}
if (System.getProperty("sun.arch.data.model").equals("64")) {
archDataModel64 = true;
}
if (isLinux) {
final File file = new File("/etc", "os-release");
try (FileInputStream fis = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis))) {
String string;
while ((string = br.readLine()) != null) {
if (string.toLowerCase().contains("raspbian")) {
if (string.toLowerCase().contains("name")) {
isPiUnix = true;
break;
}
}
}
} catch (final Exception e) {
e.printStackTrace();
}
}
}
此处以 Java42 的回答为基础是一个完整的帮助程序 class。
Java 代码中的用法
if (Raspberry.isPi()) {
...
}
测试结果
javac Raspberry.java
java Raspberry
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
Raspberry.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
* Raspberry PI helper class
* @author wf
*
*/
public class Raspberry {
public static boolean debug = false;
/**
* check if this java vm runs on a raspberry PI
*
*
* @return true if this is running on a Raspbian Linux
*/
public static boolean isPi() {
String osRelease = osRelease();
return osRelease != null && osRelease.contains("Raspbian");
}
/**
* read the first line from the given file
*
* @param file
* @return the first line
*/
public static String readFirstLine(File file) {
String firstLine = null;
try {
if (file.canRead()) {
FileInputStream fis = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(fis));
firstLine = bufferedReader.readLine();
fis.close();
}
} catch (Throwable th) {
if (debug)
th.printStackTrace();
}
return firstLine;
}
/**
* get the operating System release
*
* @return the first line from /etc/os-release or null
*/
public static String osRelease() {
String os = System.getProperty("os.name");
if (os.startsWith("Linux")) {
final File osRelease = new File("/etc", "os-release");
return readFirstLine(osRelease);
}
return null;
}
/**
* entry point to call from command line
*/
public static void main(String args[]) {
if (isPi()) {
System.out.println(osRelease());
}
}
}
这个函数会检测是否是Raspberry Pi
private static StringBuilder StringBuilderResult;
private static boolean DetectRaspBPi() {
GetInfoByExecuteCommandLinux("cat /proc/device-tree/model", false);
return StringBuilderResult.toString().toLowerCase().contains("raspberry");
}
private static void GetInfoByExecuteCommandLinux(String command, boolean getList){
try {
Process pb = new ProcessBuilder("bash", "-c", command).start();
BufferedReader reader=new BufferedReader(
new InputStreamReader(pb.getInputStream())
);
String line;
if (getList){
ListStringBuilderResult = new ArrayList<>();
} else {
StringBuilderResult = new StringBuilder();
while((line = reader.readLine()) != null)
{
if (getList){
ListStringBuilderResult.add(line);
} else {
StringBuilderResult.append(line);
}
}
} catch (Exception e){
System.out.println(e.getMessage());
}
}