EN
Java check if linux is current operating system / detect os
2 answers
4 points
How can I detect if I run my java application on Linux operating system?
I want to ensure I am on Linux before I try to read the file from Linux path:
xxxxxxxxxx
1
/root/test_file.txt
2 answers
4 points
We can use commons-lang3 library:
xxxxxxxxxx
1
import org.apache.commons.lang3.SystemUtils;
2
3
public class Program {
4
5
public static void main(String[] args) {
6
7
// Most popular usage:
8
System.out.println(SystemUtils.IS_OS_LINUX); // true // when running on Ubuntu, Debian, ...
9
System.out.println(SystemUtils.IS_OS_MAC); // false
10
11
System.out.println(SystemUtils.IS_OS_WINDOWS); // false
12
13
// Less popular usage:
14
System.out.println(SystemUtils.IS_OS_SOLARIS); // false
15
System.out.println(SystemUtils.IS_OS_SUN_OS); // false
16
}
17
}
Maven dependency:
xxxxxxxxxx
1
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
2
<dependency>
3
<groupId>org.apache.commons</groupId>
4
<artifactId>commons-lang3</artifactId>
5
<version>3.12.0</version>
6
</dependency>
Source: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.12.0
References
0 commentsShow commentsAdd comment
3 points
Custom solution:
xxxxxxxxxx
1
String osName = System.getProperty("os.name");
2
3
boolean isLinux = osName.toLowerCase().startsWith("linux");
0 commentsAdd comment