Enum Class Platform
- All Implemented Interfaces:
Serializable
,Comparable<Platform>
,Constable
Most of this information can also be obtained from system properties,
environment variables, or the System
class. However,
working directly with these properties is error-prone, since the properties
are scattered across various locations, need to be parsed by applications,
and do not always behave consistently across different platforms. In addition
to that, the standard APIs have not always been updated to reflect modern
best practices for each platform. For example, the java.util.prefs
API for storing application preferences was never updated for the introduction
of the Mac App Store in 2010, or Mac App Store sandboxing in 2014. This class
therefore provides a way to manage application data and preferences in a way
that is considered suitable and native for each platform.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic File
getApplicationData
(String app, String path) Returns aFile
handle that points to a file inside the platform's directory for storing application data, for the application with the specified name.static File
Returns the platform's directory for storing application data, for the application with the specified name.static TimeZone
Returns the default timezone.static Version
Returns the version of the Java Virtual Machine.Returns the native library path, indicated by the system propertyjava.library.path
, as a list ofFile
objects that represent each directory.static Platform
Returns the current platform family.static String
Returns the display name for the current platform, for example "Windows 11" or "macOS Ventura".static File
Returns the platform's directory for temporary files.static long
Deprecated.static String
Returns the name of the current user.static File
getUserData
(String path) Returns aFile
handle that points to a file in the platform's standard directory for storing user data (e.g.static File
Returns the platform's standard directory for storing user data (e.g.static File
Returns aFile
that points to the current user's desktop directory.static File
Returns aFile
that points to the current user's home directory.static File
Returns aFile
that points to the current working directory.static boolean
Convenience method that can be used to check the current platform.static boolean
static boolean
isLinux()
static boolean
isMac()
static boolean
Returns true if the current platform is running on a Mac and if the Java Virtual Machine is running from within the Mac App Store sandbox.static boolean
isTeaVM()
static boolean
toString()
Returns the display name for this platform family.static Platform
Returns the enum constant of this class with the specified name.static Platform[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
WINDOWS
-
MAC
-
LINUX
-
GOOGLE_CLOUD
-
ANDROID
-
IOS
-
TEAVM
-
UNKNOWN
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-
toString
Returns the display name for this platform family. Examples are "macOS", "Windows", and "Google Cloud". The platform family does not include the platform version, usegetPlatformName()
to obtain the complete display name. -
getPlatform
Returns the current platform family. This does not return the specific platform version, as most platform-specific behavior is based on the platform family rather than the specific versions.getPlatformName()
can be used to obtain the full platform display name if version-specific behavior is required.When running on TeaVM, this method will always return
TEAVM
, regardless of the underlying operating system and/or browser. -
getPlatformName
Returns the display name for the current platform, for example "Windows 11" or "macOS Ventura". Platform-specific behavior is more likely to be based on the platform family rather than the specific platform. Applications should therefore prefergetPlatform()
instead of parsing the display name string returned by this method.When running via TeaVM, this method will always return "TeaVM", regardless of the underlying operating system and/or browser.
-
is
Convenience method that can be used to check the current platform. For example, usingPlatform.is(Platform.MAC)
is a shorthand forPlatform.getPlatform().equals(Platform.MAC)
. -
isWindows
public static boolean isWindows() -
isMac
public static boolean isMac() -
isLinux
public static boolean isLinux() -
isGoogleCloud
public static boolean isGoogleCloud() -
isTeaVM
public static boolean isTeaVM() -
isMacAppStore
public static boolean isMacAppStore()Returns true if the current platform is running on a Mac and if the Java Virtual Machine is running from within the Mac App Store sandbox. -
getJavaVersion
Returns the version of the Java Virtual Machine. Examples of returned version numbers are "1.7.0_55" and "21.0.1". Detection is based on the value of thejava.version
system property. ReturnsVersion.UNKNOWN
if the current platform does not provide Java version information. -
getUsedHeapMemory
Deprecated.Returns the amount of heap memory that is currently being used by the Java Virtual Machine, in bytes. -
getNativeLibraryPath
Returns the native library path, indicated by the system propertyjava.library.path
, as a list ofFile
objects that represent each directory.- Throws:
UnsupportedOperationException
- if the current platform does not allow the application to load or access native libraries at runtime.
-
getUserWorkingDirectory
Returns aFile
that points to the current working directory.- Throws:
UnsupportedOperationException
- on platforms that do not have the concept of a working directory.
-
getUserAccount
Returns the name of the current user.- Throws:
UnsupportedOperationException
- on platforms that do not support user accounts. Examples are Android and Google App Engine.
-
getUserHomeDir
Returns aFile
that points to the current user's home directory.- Throws:
UnsupportedOperationException
- on platforms that do not support user accounts. Examples are Android and Google App Engine.
-
getUserDesktopDir
Returns aFile
that points to the current user's desktop directory. Note that this method is only available on desktop platforms, and that even then some platforms might not allow applications to write directly to the user's desktop.- Throws:
UnsupportedOperationException
- if the user desktop is not available or not accessible.
-
getApplicationDataDirectory
Returns the platform's directory for storing application data, for the application with the specified name.- Throws:
IllegalArgumentException
- If the application name cannot be used as a directory name.UnsupportedOperationException
- if the platform does not allow application data (e.g. Google App Engine).
-
getApplicationData
Returns aFile
handle that points to a file inside the platform's directory for storing application data, for the application with the specified name. If the application data directory does not already exist it will be created.- Parameters:
app
- Name of the application for which to create the directory.- Throws:
IllegalArgumentException
- If the application name cannot be used as a directory name, if the path is empty, or if an absolute path is used instead of a relative path.UnsupportedOperationException
- if the platform does not allow application data (e.g. Google App Engine).
-
getUserDataDirectory
Returns the platform's standard directory for storing user data (e.g. "My Documents" on Windows).- Throws:
UnsupportedOperationException
- if the platform does not allow access to user files (e.g. Google App Engine).
-
getUserData
Returns aFile
handle that points to a file in the platform's standard directory for storing user data (e.g. "My Documents" on Windows).- Throws:
IllegalArgumentException
- if the path is absolute.UnsupportedOperationException
- if the platform does not allow access to user files (e.g. Google App Engine).
-
getTempDir
Returns the platform's directory for temporary files.- Throws:
UnsupportedOperationException
- if the platform has no writable file system. An example is Google App Engine.
-
getDefaultTimeZone
Returns the default timezone. By default, this will return theEurope/Amsterdam
time zone. The default time zone can be set explicitly using the environment variable "COLORIZE_TIMEZONE". If the requested time zone is not available on the current platform, the GMT time zone is used instead.
-