Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Ubuntu version using Java?

Tags:

java

ubuntu

I'm trying to get Ubuntu version using Java, but there is a problem.

When using the System.getProperty("os.version"), it's return the kernel version.

  • How can I get release version like "14.04", etc?
like image 673
MBaev Avatar asked Jun 30 '26 09:06

MBaev


2 Answers

You can run any terminal command (like lsb_release -a) inside a Java class with

Runtime.getRuntime().exec("lsb_release -a");

And parse the output

EDIT: Clear solution

String[] args = new String[] {"/bin/bash", "-c", "lsb_release -r", "with", "args"};
Process proc = new ProcessBuilder(args).start();

Note: You need to use lsb_release -r to get only the version.

like image 124
Simone Celia Avatar answered Jul 02 '26 23:07

Simone Celia


Here is a snippet to achieve your goal:

String version = (new BufferedReader(new InputStreamReader((new ProcessBuilder("lsb_release", "-rs")).start().getInputStream()))).readLine();
System.out.println(version);

Those two lines display something like that:

14.04
like image 31
Alexandre Fenyo Avatar answered Jul 02 '26 23:07

Alexandre Fenyo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!