How to get os name (ubuntu) in java program [closed]
This question was closed because it is not about an official Ubuntu flavor. It is not currently accepting answers.
This is not about an official Ubuntu flavor. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow.
I want to get os name programmatically in a java program at runtime. I tried System.getProperty(«os.name») but result is «Linux» . How to do this?
1 Answer 1
I found that there is only three system properties to determine os details :
os.name : Linux os.version : 3.13.0-74-generic os.arch " amd64
ubuntu specific solution is to read these files :
$ cat /etc/*-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04 LTS" NAME="Ubuntu" VERSION="14.04, Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04 LTS" VERSION_ID="14.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
File file = new File("/etc"); File[] list = file.listFiles(new FilenameFilter() < public boolean accept(File dir, String name) < return name.endsWith("-release"); >>); for (File f : list) < Scanner s = new Scanner(new FileInputStream(f)); while (s.hasNext()) < System.out.println(s.nextLine()); >>
How can I detect a Unix-like OS in Java?
Ok, I know that System.getProperty(«os.name») will give me the name of the OS I’m running under, but that’s not a lot of help. What I need to know is if the OS I’m running on is a ‘Unix-like’ OS, I don’t care if it’s HP-UX, AIX, Mac OS X or whatever. From the list of possible os.name values it seems like a quick and dirty way of detecting a ‘Unix-like’ OS is checking if os.name does not contain «Windows». The false positives that will give me are OSes my code is very unlikely to encounter! Still, I’d love to know a better way if there is one.
Is there a particular reason you’re looking for «Unix-like»? If you’re looking for a particular feature, it might be better to check that feature.
«Yes, there are often ways where you could get around this sort of thing with pristine Java, but sometimes it’s just not worth the trouble» . Yes it IS worth the trouble. If you use Java.io.file.list, download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/… You get a directory listing and you avoid making syscalls to ls or dir and you avoid OS detection logic. Using an OS independent API call is ALWAYS better than fiddling with OS detection logic.
Just as an extra warning, trying specific commands could be unhelpful too (depending on what machines you might end up on) — uname, ls, etc all work on my Windows machine, because I have MinGW+MSYS installed.
The reason I want to detect if I’m on a Unix-like OS is because I’m creating directories which, when on Unix, I need to «chmod» to open write permissions to everyone. I don’t want (or need) to be calling «chmod» when running on Windows. AFAIK Java doesn’t have an OS independent API for changing file permissions.
9 Answers 9
Use the org.apache.commons.lang.SystemUtils utility class from Commons Lang, it has a nice IS_OS_UNIX constant. From the javadoc:
Is true if this is a POSIX compilant system, as in any of AIX, HP-UX, Irix, Linux, MacOSX, Solaris or SUN OS.
The field will return false if OS_NAME is null.
Simple, effective, easy to read, no cryptic tricks.
@Anders: First of all, let me underline that we are talking about javadoc here. Did you check the implementation before claiming it’s wrong? Second, what is utterly wrong with the list, what would you add? Last thing, feel free to submit a javadoc patch, patches are often welcome if they can improve something.
Last time I looked there were more then a fair share of different Linux distributions, even the oldest one (Slackware) is not POSIX compliant, honestly I don’t believe a single one can claim to be. Neither is quite Solaris nor SUN OS (which really should now be the same), and MacOSX is not POSIX compliant either. And no, I did not check the implementation. I will revise the first comment, hopefully the implementation is better then the documentation.
@Anders: Thanks for the feedback and clarification, I get your point (and I can agree now, the javadoc is not good).
I’ve used your scheme in production code on Windows XP, Vista, Win7, Mac OS 10.3 — 10.6 and a variety of Linux distros without an issue:
if (System.getProperty("os.name").startsWith("Windows")) < // includes: Windows 2000, Windows 95, Windows 98, Windows NT, Windows Vista, Windows XP >else < // everything else >
Essentially, detect Unix-like by not detecting Windows.
Despite the many good ideas posted (I particularly liked File.listRoots) I think this is the «correct» way to do it. False positives are highly unlikely.
Won’t this not work if someone is using an OS like ReactOS? It is certainly not Unix-like, but it’s not Windows either.
@Arin. Agreed. Eight year old answer is showing it’s age. I’ll make it community wiki and have at it.
File.listRoots() will give you an array of the file system root directories.
If you are on a Unix-like system, then the array should contain a single entry «/» and on Windows systems you’ll get something like [«C:», «D:», . ]
Edit: @chris_l: I totally forgot about mobile phones. Some digging turns up that Android returns a «/\0\0» — a slash followed by two null bytes (assumed to be a bug). Looks like we avoid false positives for the time being through luck and coincidence. Couldn’t find good data on other phones, unfortunately.
It’s probably not a good idea to run the same code on desktops and mobile phones regardless, but it is interesting to know. Looks like it comes down to needing to check for specific features instead of simply the system type.
Getting Linux Distro from java
In windows xp, it prints like : Windows XP But in ubuntu/fedora, it shows only Linux . Can anyone help me to find which linux version Iam using (like ubuntu or fedora) using java code? Is it possible to find the linux distro from java?
Be sure to add @BrianRoach (or whoever) to notify them of a new comment. BTW — what relevance is this? The user already knows which Linux distro they have (if they care enough to wonder), and the app. rarely if ever needs such information. What feature are you trying to provide through knowing that information?
Its for reading configuration file of mariadb is in different locations. For getting this file, I need the linux distro.
4 Answers 4
He is explicitly asking how to get the Linux distribution name(Like ubuntu, linux and macOS). more info here
String[] cmd = < "/bin/sh", "-c", "cat /etc/*-release" >; try < Process p = Runtime.getRuntime().exec(cmd); BufferedReader bri = new BufferedReader(new InputStreamReader( p.getInputStream())); String line = ""; while ((line = bri.readLine()) != null) < System.out.println(line); >> catch (IOException e)
It if you only need the version try with uname -a
Some linux distros contain the distro version in the /proc/version file. Here is an example to print them all from java without invoking any SO commands
//lists all the files ending with -release in the etc folder File dir = new File("/etc/"); File fileList[] = new File[0]; if(dir.exists()) < fileList = dir.listFiles(new FilenameFilter() < public boolean accept(File dir, String filename) < return filename.endsWith("-release"); >>); > //looks for the version file (not all linux distros) File fileVersion = new File("/proc/version"); if(fileVersion.exists()) < fileList = Arrays.copyOf(fileList,fileList.length+1); fileList[fileList.length-1] = fileVersion; >//prints all the version-related files for (File f : fileList) < try < BufferedReader myReader = new BufferedReader(new FileReader(f)); String strLine = null; while ((strLine = myReader.readLine()) != null) < System.out.println(strLine); >myReader.close(); > catch (Exception e) < System.err.println("Error: " + e.getMessage()); >>