Java Tutorial/Development/OS
Содержание
Class representing a standard operating system platform, WIN, MAC, or POSIX.
/*
* OS.java
* 2007-04-14
*/
//cb.aloe.util;
import java.io.File;
import java.util.ArrayList;
/**
* Class representing a standard operating system platform, WIN, MAC, or POSIX.
*
* @author Christopher Bach
*/
public final class OS {
public static final OS WIN = new OS("WIN");
public static final OS MAC = new OS("MAC");
public static final OS POSIX = new OS("POSIX");
private String ourOS = "";
/**
* Private constructor to avert instantiation.
*/
private OS(String os) {
ourOS = os;
}
/**
* Returns a String representing this OS object.
*/
public String toString() {
return ourOS;
}
/**
* Returns the first readable and writable application data folder appropriate
* to this OS.
*/
public static File getAppDataFolder() {
File[] folders = listAppDataFolders();
for (int i = 0; i < folders.length; i++) {
File folder = folders[i];
if (folder.canRead() && folder.canWrite())
return folder;
}
return null;
}
/**
* Returns the first readable and writable user data folder appropriate to
* this OS.
*/
public static File getUserDataFolder() {
File[] folders = listUserDataFolders();
for (int i = 0; i < folders.length; i++) {
File folder = folders[i];
if (folder.canRead() && folder.canWrite())
return folder;
}
return null;
}
/**
* Returns a list of the preferred locations for storing application- specific
* data in descending order.
*/
public static File[] listAppDataFolders() {
String home = System.getProperty("user.home");
ArrayList folders = new ArrayList();
if (isWinNT()) // NT/2000/XP
{
// C:\Documents and Settings\All Users\Application Data
// Surmise that the "All Users" folder will be a child of the
// parent of the current user"s home folder:
File folder = new File(home).getParentFile();
folders.add(new File(folder, "All Users\\Application Data"));
}
else if (isWin9X()) // 95/98/ME
{
// C:\Windows
folders.add(new File(home));
}
else if (isVista()) {
// C:\ProgramData
File folder = new File(home).getParentFile().getParentFile();
folders.add(new File(folder, "ProgramData"));
// C:\Users\Public\AppData
folder = new File(home).getParentFile();
folders.add(new File(folder, "Public\\AppData"));
}
else if (isMac()) {
folders.add(new File("/Library/Application Support"));
}
else {
folders.add(new File("/var/local"));
folders.add(new File("/var"));
}
// folders.addAll(Arrays.asList(listUserDataFolders()));
File[] files = new File[folders.size()];
return (File[]) folders.toArray(files);
}
/**
* Returns a list of the preferred locations for storing user- specific data
* in descending order.
*/
public static File[] listUserDataFolders() {
String home = System.getProperty("user.home");
ArrayList folders = new ArrayList();
if (isWinNT()) {
folders.add(new File(home + "\\Application Data"));
folders.add(new File(home + "\\Local Settings\\Application Data"));
}
else if (isVista()) {
folders.add(new File(home + "\\AppData"));
folders.add(new File(home + "\\AppData\\Local"));
}
else if (isMac()) {
folders.add(new File(home + "/Library/Application Support"));
}
folders.add(new File(home));
File[] files = new File[folders.size()];
return (File[]) folders.toArray(files);
}
/**
* Returns the name of the current operating system platform as listed in the
* System properties.
*/
public static final String getOSName() {
return System.getProperty("os.name");
}
/**
* Returns the OS representing the current operating system platform.
*/
public static final OS getOS() {
String os = System.getProperty("os.name").toLowerCase();
if (os.indexOf("windows") >= 0)
return WIN;
else if (os.indexOf("mac os") >= 0)
return MAC;
else
return POSIX;
}
/**
* Returns whether or not the current operating system is a Microsoft Windows
* platform.
*/
public static final boolean isWindows() {
return getOS() == WIN;
}
/**
* Returns whether or not the current operating system is a Microsoft Windows
* 9X platform (Windows 95/98/ME).
*/
public static boolean isWin9X() {
if (getOS() != WIN)
return false;
String os = getOSName();
if (os.indexOf("95") >= 0 || os.indexOf("98") >= 0 || os.indexOf("me") >= 0)
return true;
return false;
}
/**
* Returns whether or not the current operating system is a Microsoft Windows
* NT platform (Windows NT/2000/2003/XP).
*/
public static boolean isWinNT() {
if (getOS() != WIN)
return false;
String os = getOSName();
if (os.indexOf("NT") >= 0 || os.indexOf("2000") >= 0 || os.indexOf("2003") >= 0
|| os.indexOf("XP") >= 0)
return true;
return false;
}
/**
* Returns whether or not the current operating system is a Microsoft Windows
* Vista platform.
*/
public static boolean isVista() {
if (getOS() != WIN)
return false;
String os = getOSName();
return (os.indexOf("Vista") >= 0);
}
/**
* Returns whether or not the current operating system is the Apple Macintosh
* OS X platform.
*/
public static final boolean isMac() {
return getOS() == MAC;
}
/**
* Returns whether or not the current operating system is a Posix-compatible
* platform (Unix, Linux, Solaris, etc).
*/
public static final boolean isPosix() {
return getOS() == POSIX;
}
}
Class to help determining the OS
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
/**
* Class to help determining the OS.
*
* @author
* @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
*/
final class OsFamily
{
private final String name;
private final OsFamily[] families;
OsFamily(final String name)
{
this.name = name;
families = new OsFamily[0];
}
OsFamily(final String name, final OsFamily[] families)
{
this.name = name;
this.families = families;
}
/**
* Returns the name of this family.
*/
public String getName()
{
return name;
}
/**
* Returns the OS families that this family belongs to.
*/
public OsFamily[] getFamilies()
{
return families;
}
}
Condition that tests the OS type.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import java.util.Locale;
/**
* Condition that tests the OS type.
*/
public final class OS {
private static final String FAMILY_OS_400 = "os/400";
private static final String FAMILY_Z_OS = "z/os";
private static final String FAMILY_WIN9X = "win9x";
private static final String FAMILY_OPENVMS = "openvms";
private static final String FAMILY_UNIX = "unix";
private static final String FAMILY_TANDEM = "tandem";
private static final String FAMILY_MAC = "mac";
private static final String FAMILY_DOS = "dos";
private static final String FAMILY_NETWARE = "netware";
private static final String FAMILY_OS_2 = "os/2";
private static final String FAMILY_WINDOWS = "windows";
private static final String OS_NAME = System.getProperty("os.name")
.toLowerCase(Locale.US);
private static final String OS_ARCH = System.getProperty("os.arch")
.toLowerCase(Locale.US);
private static final String OS_VERSION = System.getProperty("os.version")
.toLowerCase(Locale.US);
private static final String PATH_SEP = System.getProperty("path.separator");
/**
* Default constructor
*/
private OS() {
}
/**
* Determines if the OS on which Ant is executing matches the given OS
* family. * Possible values:<br />
* <ul>
* <li>dos</li>
* <li>mac</li>
* <li>netware</li>
* <li>os/2</li>
* <li>tandem</li>
* <li>unix</li>
* <li>windows</li>
* <li>win9x</li>
* <li>z/os</li>
* <li>os/400</li>
* </ul>
*
* @param family
* the family to check for
* @return true if the OS matches
*/
private static boolean isFamily(final String family) {
return isOs(family, null, null, null);
}
public static boolean isFamilyDOS() {
return isFamily(FAMILY_DOS);
}
public static boolean isFamilyMac() {
return isFamily(FAMILY_MAC);
}
public static boolean isFamilyNetware() {
return isFamily(FAMILY_NETWARE);
}
public static boolean isFamilyOS2() {
return isFamily(FAMILY_OS_2);
}
public static boolean isFamilyTandem() {
return isFamily(FAMILY_TANDEM);
}
public static boolean isFamilyUnix() {
return isFamily(FAMILY_UNIX);
}
public static boolean isFamilyWindows() {
return isFamily(FAMILY_WINDOWS);
}
public static boolean isFamilyWin9x() {
return isFamily(FAMILY_WIN9X);
}
public static boolean isFamilyZOS() {
return isFamily(FAMILY_Z_OS);
}
public static boolean isFamilyOS400() {
return isFamily(FAMILY_OS_400);
}
public static boolean isFamilyOpenVms() {
return isFamily(FAMILY_OPENVMS);
}
/**
* Determines if the OS on which Ant is executing matches the given OS name.
*
* @param name
* the OS name to check for
* @return true if the OS matches
*/
public static boolean isName(final String name) {
return isOs(null, name, null, null);
}
/**
* Determines if the OS on which Ant is executing matches the given OS
* architecture.
*
* @param arch
* the OS architecture to check for
* @return true if the OS matches
*/
public static boolean isArch(final String arch) {
return isOs(null, null, arch, null);
}
/**
* Determines if the OS on which Ant is executing matches the given OS
* version.
*
* @param version
* the OS version to check for
* @return true if the OS matches
*/
public static boolean isVersion(final String version) {
return isOs(null, null, null, version);
}
/**
* Determines if the OS on which Ant is executing matches the given OS
* family, name, architecture and version
*
* @param family
* The OS family
* @param name
* The OS name
* @param arch
* The OS architecture
* @param version
* The OS version
* @return true if the OS matches
*/
public static boolean isOs(final String family, final String name,
final String arch, final String version) {
boolean retValue = false;
if (family != null || name != null || arch != null || version != null) {
boolean isFamily = true;
boolean isName = true;
boolean isArch = true;
boolean isVersion = true;
if (family != null) {
if (family.equals(FAMILY_WINDOWS)) {
isFamily = OS_NAME.indexOf(FAMILY_WINDOWS) > -1;
} else if (family.equals(FAMILY_OS_2)) {
isFamily = OS_NAME.indexOf(FAMILY_OS_2) > -1;
} else if (family.equals(FAMILY_NETWARE)) {
isFamily = OS_NAME.indexOf(FAMILY_NETWARE) > -1;
} else if (family.equals(FAMILY_DOS)) {
isFamily = PATH_SEP.equals(";")
&& !isFamily(FAMILY_NETWARE);
} else if (family.equals(FAMILY_MAC)) {
isFamily = OS_NAME.indexOf(FAMILY_MAC) > -1;
} else if (family.equals(FAMILY_TANDEM)) {
isFamily = OS_NAME.indexOf("nonstop_kernel") > -1;
} else if (family.equals(FAMILY_UNIX)) {
isFamily = PATH_SEP.equals(":")
&& !isFamily(FAMILY_OPENVMS)
&& (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x"));
} else if (family.equals(FAMILY_WIN9X)) {
isFamily = isFamily(FAMILY_WINDOWS)
&& (OS_NAME.indexOf("95") >= 0
|| OS_NAME.indexOf("98") >= 0
|| OS_NAME.indexOf("me") >= 0 || OS_NAME
.indexOf("ce") >= 0);
} else if (family.equals(FAMILY_Z_OS)) {
isFamily = OS_NAME.indexOf(FAMILY_Z_OS) > -1
|| OS_NAME.indexOf("os/390") > -1;
} else if (family.equals(FAMILY_OS_400)) {
isFamily = OS_NAME.indexOf(FAMILY_OS_400) > -1;
} else if (family.equals(FAMILY_OPENVMS)) {
isFamily = OS_NAME.indexOf(FAMILY_OPENVMS) > -1;
} else {
throw new IllegalArgumentException(
"Don\"t know how to detect os family \"" + family
+ "\"");
}
}
if (name != null) {
isName = name.equals(OS_NAME);
}
if (arch != null) {
isArch = arch.equals(OS_ARCH);
}
if (version != null) {
isVersion = version.equals(OS_VERSION);
}
retValue = isFamily && isName && isArch && isVersion;
}
return retValue;
}
}
Determine the JDK Version Number
/** Determine the JDK Version Number
*
* There is a human readible JDK version number available as a Java property. The property name is called "java.version".
* Unfortunately it is complicated to parse it mechanically. A somewhat better choice is the Java property called
* "java.class.version". The property value can be converted to a floating pointer value. JDK 1.1 implies 45.3. JDK 1.2
* implies 46.0. JDK 1.3 implies 47.0. JDK 1.4 implies 48.0.
*
* java.class.version property value
* <ul>
* <li> JDK 1.1 = 45.3
* <li> JDK 1.2 = 46.0
* <li> JDK 1.3 = 47.0
* <li> JDK 1.4 = 48.0
* </ul>
*
* @author Thomas Rischbeck <thomas.rischbeck@arjuna.ru>
* @version $Id: VersionCheck.java 2342 2006-03-30 13:06:17Z $
*/
public class VersionCheck {
private static final String CLASS_VERSION = System.getProperty("java.class.version","44.0");
private static final boolean IS_JDK_11 = ("46.0".rupareTo(CLASS_VERSION) > 0) && ("45.3".rupareTo(CLASS_VERSION) <= 0);
private static final boolean IS_JDK_12 = ("47.0".rupareTo(CLASS_VERSION) > 0) && ("46.0".rupareTo(CLASS_VERSION) <= 0);
private static final boolean IS_JDK_13 = ("48.0".rupareTo(CLASS_VERSION) > 0) && ("47.0".rupareTo(CLASS_VERSION) <= 0);
private static final boolean IS_JDK_14 = ("49.0".rupareTo(CLASS_VERSION) > 0) && ("48.0".rupareTo(CLASS_VERSION) <= 0);
private static final boolean IS_JDK_13_OR_BELOW = IS_JDK_11 || IS_JDK_12 || IS_JDK_13;
private static final boolean IS_JDK_14_OR_ABOVE = ("48.0".rupareTo(CLASS_VERSION) <= 0);
public static final boolean isJDK11only()
{
return IS_JDK_11;
}
public static final boolean isJDK12only()
{
return IS_JDK_12;
}
public static final boolean isJDK13only()
{
return IS_JDK_13;
}
public static final boolean isJDK14only()
{
return IS_JDK_14;
}
public static final boolean isJDK13orBelow()
{
return IS_JDK_13_OR_BELOW;
}
public static final boolean isJDK14orAbove()
{
return IS_JDK_14_OR_ABOVE;
}
}
Get OS
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don"t indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/*
* OS.java
*
* Created on December 8, 2001, 5:48 PM
*/
import java.io.File;
/**
*
* @author bnevins
* @version
*/
public class OS
{
private OS()
{
}
///////////////////////////////////////////////////////////////////////////
public static boolean isWindows()
{
return File.separatorChar == "\\";
}
///////////////////////////////////////////////////////////////////////////
public static boolean isUNIX()
{
return File.separatorChar == "/";
}
///////////////////////////////////////////////////////////////////////////
public static boolean isUnix()
{
// convenience method...
return isUNIX();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isSun()
{
return isName("sun");
}
///////////////////////////////////////////////////////////////////////////
public static boolean isSolaris10()
{
return isSun() && isVersion("5.10");
}
///////////////////////////////////////////////////////////////////////////
public static boolean isSunSparc()
{
return isName("sun") && isArch("sparc");
}
///////////////////////////////////////////////////////////////////////////
public static boolean isSunX86()
{
return isName("sun") && isArch("x86");
}
///////////////////////////////////////////////////////////////////////////
public static boolean isLinux()
{
return isName("linux");
}
///////////////////////////////////////////////////////////////////////////
public static boolean isDarwin()
{
return isName("Mac OS X");
}
///////////////////////////////////////////////////////////////////////////
public static boolean isWindowsForSure()
{
return isName("windows") && isWindows();
}
///////////////////////////////////////////////////////////////////////////
// There are 10 known Linux versions!
///////////////////////////////////////////////////////////////////////////
public static boolean isDebianLinux()
{
return isLinux() && new File("/etc/debian_version").exists();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isFedoraLinux()
{
return isLinux() && new File("/etc/fedora-release").exists();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isGentooLinux()
{
return isLinux() && new File("/etc/gentoo-release").exists();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isKnoppixLinux()
{
return isLinux() && new File("/etc/knoppix_version").exists();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isMandrakeLinux()
{
return isLinux() && new File("/etc/mandrake-release").exists();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isMandrivaLinux()
{
return isLinux() && new File("/etc/mandriva-release").exists();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isRedHatLinux()
{
return isLinux() && new File("/etc/redhat-release").exists();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isSlackwareLinux()
{
return isLinux() && new File("/etc/slackware-version").exists();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isSuSELinux()
{
return isLinux() && new File("/etc/SuSE-release").exists();
}
///////////////////////////////////////////////////////////////////////////
public static boolean isUbuntuLinux()
{
return isLinux() && new File("/etc/lsb-release").exists();
}
///////////////////////////////////////////////////////////////////////////
private static boolean isArch(String name)
{
String archname = System.getProperty("os.arch");
if(archname == null || archname.length() <= 0)
return false;
// case insensitive compare...
archname= archname.toLowerCase();
name= name.toLowerCase();
if(archname.indexOf(name) >= 0)
return true;
return false;
}
///////////////////////////////////////////////////////////////////////////
private static boolean isName(String name)
{
String osname = System.getProperty("os.name");
if(osname == null || osname.length() <= 0)
return false;
// case insensitive compare...
osname = osname.toLowerCase();
name = name.toLowerCase();
if(osname.indexOf(name) >= 0)
return true;
return false;
}
///////////////////////////////////////////////////////////////////////////
private static boolean isVersion(String version)
{
String osversion = System.getProperty("os.version");
if(osversion == null || osversion.length() <= 0 || version == null || version.length() <= 0 )
return false;
if(osversion.equals(version))
return true;
return false;
}
///////////////////////////////////////////////////////////////////////////
public static final String WINDOWS_BATCH_FILE_EXTENSION = ".bat";
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
System.out.println("os.version = " + System.getProperty("os.version"));
System.out.println("os.name = " + System.getProperty("os.name"));
System.out.println("os.arch = " + System.getProperty("os.arch"));
System.out.println("isUNIX() returned: " + isUNIX());
System.out.println("isWindows() returned: " + isWindows());
System.out.println("isWindowsForSure() returned: " + isWindowsForSure());
System.out.println("isSun() returned: " + isSun());
System.out.println("isLinux() returned: " + isLinux());
System.out.println("isDebianLinux() returned: " + isDebianLinux());
System.out.println("isFedoraLinux() returned: " + isFedoraLinux());
System.out.println("isGentooLinux() returned: " + isGentooLinux());
System.out.println("isKnoppixLinux() returned: " + isKnoppixLinux());
System.out.println("isMandrakeLinux() returned: " + isMandrakeLinux());
System.out.println("isMandrivaLinux() returned: " + isMandrivaLinux());
System.out.println("isRedHatLinux() returned: " + isRedHatLinux());
System.out.println("isSlackwareLinux() returned: " + isSlackwareLinux());
System.out.println("isSuSELinux() returned: " + isSuSELinux());
System.out.println("isUbuntuLinux() returned: " + isUbuntuLinux());
System.out.println("isSunX86() returned: " + isSunX86());
System.out.println("isSunSparc() returned: " + isSunSparc());
System.out.println("isDarwin() returned: " + isDarwin());
System.out.println("isSolaris10() returned: " + isSolaris10());
}
}