Carles Lloret Salvo Carles Lloret Salvo - 2 months ago 13
Java Question

The ideal way to detect a raspberry pi from java (jar)

Hello I would like to know which is the ideal way to detect if a jar/java program is running on raspberry pi or not.

Why do I want that? I want to use the watchdog in the raspberry pi but also I use the java program from windows which doesn't or doesn't require the watchdog.

Is there a way to detect if the jar is running on a raspberry the same way some people detect the operative system?

The same way people use that...

class ShowProperties {
public static void main(String[] args) {
System.getProperties().list(System.out);
}
}

Answer

You should be able to work out details of the operating system your JVM is running on by testing the values of these Java system properties:

  • os.name Operating system name
  • os.arch Operating system architecture
  • os.version Operating system version

Use System.getProperty(name) to fetch the value of a system property.


If that isn't sufficiently precise, then you need to resort to non-portable solutions, such as using System.exec(...) to run uname -1 or similar.

Note: the "raspberrypi" you see on your system is actually the default hostname, and is not a reliable indicator. (It will often be set by the user to something else.)