The Daily WTF ([info]dailywtf_rss) rakstīja,
@ 2013-12-23 11:15:00

Previous Entry  Add to memories!  Tell a Friend!  Next Entry
CodeSOD: Where's Windows?

Java was touted as write once, run anywhere to emphasize its cross-platform advantages. Of course, the devil is in the details, and to interact with the local operating system, you still need to know something about it.

Without a built-in facility, the only real way to figure out the local OS is to try to exec commands unique to each OS and then check to see which ones succeed, and what value(s) get returned. For example, uname -a provides useful information on *nix systems, but is useless on Windows. It can be as painful as trying to find Waldo.

Fortunately, since different operating systems tend to have their own unique command sets and such, the designers of Java built in a convenient way of discerning the OS on which the JVM is running:

  String osName = System.getProperties().getProperty("os.name");

The values returned by the above snippet are:

...and probably a few others.

How folks choose to use this information is where things start to fall apart.

For example, if you wanted to run a command in a subshell, in Win 95/98/... you run: command.com. In Win NT and forward, it's: cmd.exe. In *nix, it's: {c | k | tc}sh.exe, and so forth.

A. B.'s coworkers came up with:

  if (osName.indexOf("Windows 9") > -1) {  // Win-95, 98
     cmdBase = new String[3]; 
     cmdBase[0] = "command.com"; 
     cmdBase[1] = "/c"; 
     cmdBase[2] = "set"; 
  } else if ((osName.indexOf("NT")           > -1) || 
             (osName.indexOf("Windows 2000") > -1) ) { 
     cmdBase = new String[3]; 
     cmdBase[0] = "cmd.exe"; 
     cmdBase[1] = "/c"; 
     cmdBase[2] = "set"; 
  } else {// our last hope, we assume Unix 
     cmdBase = new String[3]; 
     String[] cmdConfigValue = new String[5]; 
     cmdConfigValue[0]=getProfileString("SYSTEM", "UNIX_ENV0", "/usr/bin/env"); 
     cmdConfigValue[1]=getProfileString("SYSTEM", "UNIX_ENV1", ""); 
     cmdConfigValue[2]=getProfileString("SYSTEM", "UNIX_ENV2", ""); 
     cmdConfigValue[3]=getProfileString("SYSTEM", "UNIX_SHELL0", "/bin/sh"); 
     cmdConfigValue[4]=getProfileString("SYSTEM", "UNIX_SHELL1", "-c");
     // construct cmdBase from the pieces
  }

I wonder what they'll do when Windows 9 comes out?

[Advertisement] BuildMaster 4.0 is here! Check out the brand-new UI and see how you can deploy directly from TeamCity (and other CI) to your own servers, the cloud, and more.


Neesi iežurnalējies. Iežurnalēties?