0

I understand that you can do

SystemUtils.IS_OS_WINDOWS_VISTA || SystemUtils.IS_OS_WINDOWS_7

but is there a way to do this programatically for all future versions windows, so that the method is something like:

isWindowsVistaOrHigher()
Stephane Grenier
  • 15,527
  • 38
  • 117
  • 192
  • 2
    Why should Java care, as long as there's a suitable JVM? – duffymo Mar 10 '12 at 19:53
  • There are some vista/windows 7 OS features that I want to leverage when possible that aren't available in older versions of Windows. So I need to do only call this code if it's Vista, Windows 7, or higher. – Stephane Grenier Mar 10 '12 at 19:55
  • *"some .. features"* Like what, specifically? Why won't try/catch be enough to wort the difference? – Andrew Thompson Mar 10 '12 at 20:00
  • @Jeffrey: Not exactly. Not just Vista || Windows 7 but Vista || orhigher – Stephane Grenier Mar 10 '12 at 20:02
  • @AndrewThompson Try catch is not enough. – Stephane Grenier Mar 10 '12 at 20:02
  • A little louder this time ***Like what, specifically?*** Or is guessing part of the 'fun' of helping you? – Andrew Thompson Mar 10 '12 at 20:03
  • 1
    @AndrewThompson To be honest, the why isn't important as much as the how. I'd tell you more about the why but it's actually quite complex and long, and has to do with a lot of historical code that can't be changed. I don't prefer going this route, but I don't really have a choice – Stephane Grenier Mar 10 '12 at 20:14
  • @StephaneGrenier In your example, I don't think this will *ever* return `true`: `SystemUtils.IS_OS_WINDOWS_VISTA && SystemUtils.IS_OS_WINDOWS_7` – Jeffrey Mar 10 '12 at 20:17
  • @Jeffrey Yes sorry, that's right – Stephane Grenier Mar 10 '12 at 20:20
  • I don't know if you can check this like you want but wouldn't it be an alternative to check if it's **not** a version < Win Vista, e.g. if(!isWin95 && !isWin98 && !isWinMe && ...){// your code} – SimonSez Mar 10 '12 at 22:43
  • @SimonSez That's what I'm doing right now. I'm just not a fan of that solution because I don't if it's sustainable. But that's my current solution ;) – Stephane Grenier Mar 10 '12 at 23:14
  • Why wouldn't it be sustainable? It would only not be sustainable if a newer version of Windows magically returned true for one of those values. I don't see why that would happen. – AHungerArtist Mar 10 '12 at 23:48
  • I guess that's the answer then. Just check that it's not one of the early systems, one by one. Whoever wants to be the first to answer that I'll mark as the answer. – Stephane Grenier Mar 12 '12 at 04:16

2 Answers2

1

I think you don't need Apache Commons at all. The system property os.version contains the internal OS version (os.name is the human-readable name and will contain Windows followed by more specific info such as Vista). I think on Windows 7 if you call System.getProperty("os.version") you will get 6.1 while Vista is 6.0. Assuming they don't change the convention, you could try to take the part up to the first dot if a dot is present (using String.substring() and String.indexOf('.')), parse the substring as an integer using Integer.parseInt() and compare if the resulting value is at least 6.

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
1

What I ended up doing was:

public boolean isVistaOrHigher()
{
   return !SystemUtils.IS_OS_WINDOWS_XP && !SystemUtils.IS_OS_WINDOWS_98 && ... ;
}
Stephane Grenier
  • 15,527
  • 38
  • 117
  • 192
  • This assumes you are running on Windows. Look at the source for SystemUtils, it is just doing a simple check on os.version and os.name. This might be what you want `SystemUtils.IS_OS_WINDOWS && Double.parseDouble(SystemUtils.OS_VERSION) >= 6.0`. I would at least add SystemUtils.IS_OS_WINDOWS to your check. – Patrick Mar 15 '12 at 22:52
  • @Patrick I actually do a IS_OS_WINDOWS first because we also support the Mac OS, I just didn't add it because not everyone needs it and I felt it just added more noise to the answer ;) Good catch though :) – Stephane Grenier Mar 16 '12 at 17:50