# Util

Import: `import com.install4j.api.Util;`

```java
public class Util
```

The class contains various utility functions to be used by
script properties, custom actions and custom screens.

## Methods

| Signature | Returns | Description |
|---|---|---|
| [`getUserHome()`](#getuserhome) | `String` | Returns the home directory. |
| [`isSolaris()`](#issolaris) | `boolean` | Returns whether the platform is Solaris. |
| [`isLinux()`](#islinux) | `boolean` | Returns whether the platform is Linux. |
| [`isWindows()`](#iswindows) | `boolean` | Returns whether the platform is Windows. |
| [`isWindows9X()`](#iswindows9x) | `boolean` | Returns whether the platform is Windows 9X (95, 98, ME). |
| [`isWindowsNT()`](#iswindowsnt) | `boolean` | Returns whether the platform is Windows NT. |
| [`isAtLeastWindowsVista()`](#isatleastwindowsvista) | `boolean` | Returns whether the platform is at least Windows Vista. |
| [`isAtLeastWindows7()`](#isatleastwindows7) | `boolean` | Returns whether the platform is at least Windows 7. |
| [`isAtLeastWindowsXP()`](#isatleastwindowsxp) | `boolean` | Returns whether the platform is at least Windows XP. |
| [`isWindowsVista()`](#iswindowsvista) | `boolean` | Returns whether the platform is Windows Vista. |
| [`isWindows7()`](#iswindows7) | `boolean` | Returns whether the platform is Windows 7. |
| [`isWindows8()`](#iswindows8) | `boolean` | Returns whether the platform is Windows 8. |
| [`isWindows10()`](#iswindows10) | `boolean` | Returns whether the platform is Windows 10. |
| [`isAtLeastWindows10()`](#isatleastwindows10) | `boolean` | Returns whether the platform is at least Windows 10. |
| [`isWindowsXP()`](#iswindowsxp) | `boolean` | Returns whether the platform is Windows XP. |
| [`isWindows2000()`](#iswindows2000) | `boolean` | Returns whether the platform is Windows 2000. |
| [`isWindows2003()`](#iswindows2003) | `boolean` | Returns whether the platform is Windows 2003. |
| [`isWindows2008()`](#iswindows2008) | `boolean` | Returns whether the platform is Windows 2008. |
| [`isWindows2012()`](#iswindows2012) | `boolean` | Returns whether the platform is Windows 2012. |
| [`isWindows2016()`](#iswindows2016) | `boolean` | Returns whether the platform is Windows 2016. |
| [`is64BitWindows()`](#is64bitwindows) | `boolean` | Returns whether the Windows is a 64-bit Windows, regardless of whether the installer is running with a 32-bit JVM or a 64-bit JVM. |
| [`isMacOS()`](#ismacos) | `boolean` | Returns whether the platform is macOS. |
| [`isHpux()`](#ishpux) | `boolean` | Returns whether the platform is HP UX. |
| [`isAix()`](#isaix) | `boolean` | Returns whether the platform is AIX. |
| [`isWindowsInstaller()`](#iswindowsinstaller) | `boolean` | Returns whether the installer is a Windows installer |
| [`isUnixInstaller()`](#isunixinstaller) | `boolean` | Returns whether the installer is a Unix installer |
| [`isMacosInstaller()`](#ismacosinstaller) | `boolean` | Returns whether the installer is a macOS installer |
| [`getStandardApplicationsDirectory()`](#getstandardapplicationsdirectory) | `String` | Returns the standard directory for installing applications. |
| [`isArchive()`](#isarchive) | `boolean` | Returns whether the application was installed from an archive or an installer media type. |
| [`showUrl(URL)`](#showurl-url) | `void` | Show a URL in the default browser. |
| [`showMessage(String, int)`](#showmessage-string-int) | `void` | Show a message dialog. |
| [`showMessage(String)`](#showmessage-string) | `void` | Show a message dialog. |
| [`showErrorMessage(String)`](#showerrormessage-string) | `void` | Show a message dialog. |
| [`showWarningMessage(String)`](#showwarningmessage-string) | `void` | Show a message dialog. |
| [`showOptionDialog(String, String[], int)`](#showoptiondialog-string-string-int) | `int` | Show an option dialog. |
| [`fatalError(Throwable)`](#fatalerror-throwable) | `void` | Alias for [`Context.handleCriticalException`](context/Context.md#handlecriticalexception-throwable). |
| [`getParentWindow()`](#getparentwindow) | `Window` | Get the window of the installer application. |
| [`logInfo(Object, String)`](#loginfo-object-string) | `void` | Writes an info message to the installation log. |
| [`logError(Object, String)`](#logerror-object-string) | `void` | Writes an error message to the installation log. |
| [`log(Throwable)`](#log-throwable) | `void` | Writes a stack trace to the installation log. |
| [`dumpVariables(Context)`](#dumpvariables-context) | `void` | Dump all defined installer variables to stderr. |
| [`isAdminUser()`](#isadminuser) | `boolean` | Checks whether the current user has administration privileges. |
| [`isAdminGroup()`](#isadmingroup) | `boolean` | Checks whether the current user is from the administration group. |
| [`hasFullAdminRights()`](#hasfulladminrights) | `boolean` | Checks whether the current process has full administration rights. |
| [`hasAllUserInstallationRights()`](#hasalluserinstallationrights) | `boolean` | Checks whether the current process has the necessary rights to perform an installation for all users. |
| [`getAnnotatedStackTrace(Throwable)`](#getannotatedstacktrace-throwable) | `String` | If a stack trace comes from scripts, it can be difficult to trace them to their origin by looking at the stack trace alone. |
| [`printAnnotatedStackTrace(Throwable)`](#printannotatedstacktrace-throwable) | `void` | Prints the return value of [`Util.getAnnotatedStackTrace`](Util.md#getannotatedstacktrace-throwable) to `System.err`. |
| [`showPath(String)`](#showpath-string) | `void` | Show the specified directory or file. |
| [`isDirectoryWritable(File)`](#isdirectorywritable-file) | `boolean` | Checks if a directory is writable with the currently available privileges. |
| [`getLogFile()`](#getlogfile) | `File` | Get the location of the log file. |
| [`moveLogFile(File)`](#movelogfile-file) | `void` | Move the log file to a new location. |
| [`loadNativeFromResources(String)`](#loadnativefromresources-string) | `void` | Call `System#load` with the supplied file name resolved against the resource directory. |

### getUserHome()

```java
public static String getUserHome()
```

Returns the home directory.


**Returns:** the home directory

### isSolaris()

```java
public static boolean isSolaris()
```

Returns whether the platform is Solaris.


**Returns:** the result

### isLinux()

```java
public static boolean isLinux()
```

Returns whether the platform is Linux.


**Returns:** the result

### isWindows()

```java
public static boolean isWindows()
```

Returns whether the platform is Windows.


**Returns:** the result

### isWindows9X()

```java
@Deprecated public static boolean isWindows9X()
```

> **⚠ Deprecated.** always returns `false` because launchers do not work with Windows 9X anymore

Returns whether the platform is Windows 9X (95, 98, ME).


**Returns:** the result

**Deprecated.** always returns `false` because launchers do not work with Windows 9X anymore

### isWindowsNT()

```java
@Deprecated public static boolean isWindowsNT()
```

> **⚠ Deprecated.**

Returns whether the platform is Windows NT.


**Returns:** the result

### isAtLeastWindowsVista()

```java
public static boolean isAtLeastWindowsVista()
```

Returns whether the platform is at least Windows Vista.


**Returns:** the result

### isAtLeastWindows7()

```java
public static boolean isAtLeastWindows7()
```

Returns whether the platform is at least Windows 7.


**Returns:** the result

### isAtLeastWindowsXP()

```java
@Deprecated public static boolean isAtLeastWindowsXP()
```

> **⚠ Deprecated.**

Returns whether the platform is at least Windows XP.


**Returns:** the result

### isWindowsVista()

```java
public static boolean isWindowsVista()
```

Returns whether the platform is Windows Vista.


**Returns:** the result

### isWindows7()

```java
public static boolean isWindows7()
```

Returns whether the platform is Windows 7.


**Returns:** the result

### isWindows8()

```java
public static boolean isWindows8()
```

Returns whether the platform is Windows 8. This includes Windows 8.1.


**Returns:** the result

### isWindows10()

```java
public static boolean isWindows10()
```

Returns whether the platform is Windows 10.


**Returns:** the result

### isAtLeastWindows10()

```java
public static boolean isAtLeastWindows10()
```

Returns whether the platform is at least Windows 10.


**Returns:** the result

### isWindowsXP()

```java
public static boolean isWindowsXP()
```

Returns whether the platform is Windows XP.


**Returns:** the result

### isWindows2000()

```java
public static boolean isWindows2000()
```

Returns whether the platform is Windows 2000.


**Returns:** the result

### isWindows2003()

```java
public static boolean isWindows2003()
```

Returns whether the platform is Windows 2003.


**Returns:** the result

### isWindows2008()

```java
public static boolean isWindows2008()
```

Returns whether the platform is Windows 2008.


**Returns:** the result

### isWindows2012()

```java
public static boolean isWindows2012()
```

Returns whether the platform is Windows 2012.


**Returns:** the result

### isWindows2016()

```java
public static boolean isWindows2016()
```

Returns whether the platform is Windows 2016.


**Returns:** the result

### is64BitWindows()

```java
public static boolean is64BitWindows()
```

Returns whether the Windows is a 64-bit Windows, regardless of whether the installer is running with a 32-bit JVM or
a 64-bit JVM. This condition cannot be found out by inspecting `System.getProperty("os.arch")`, since this will
return a value that corresponds to the JRE and not to the OS, for example, always "x86" for a 32-bit JRE, even if it is running
on a 64-bit Windows.

This method inspects the environment variables `PROCESSOR_ARCHITECTURE` and `PROCESSOR_ARCHITEW6432`.
If `PROCESSOR_ARCHITECTURE` contains `AMD64`, `IA64` or `ARM64`, the currently used JRE is a 64-bit JRE which is then
by definition running on a 64-bit Windows. If `PROCESSOR_ARCHITECTURE` contains `x86`, the `PROCESSOR_ARCHITEW6432`
is undefined for a 32-bit Windows and contains `AMD64`, `IA64` or `ARM64` for a 64-bit Windows.


**Returns:** the result. If the current OS is not Windows, the result is always `false`.

### isMacOS()

```java
public static boolean isMacOS()
```

Returns whether the platform is macOS.


**Returns:** the result

### isHpux()

```java
public static boolean isHpux()
```

Returns whether the platform is HP UX.


**Returns:** the result

### isAix()

```java
public static boolean isAix()
```

Returns whether the platform is AIX.


**Returns:** the result

### isWindowsInstaller()

```java
public static boolean isWindowsInstaller()
```

Returns whether the installer is a Windows installer


**Returns:** the result

### isUnixInstaller()

```java
public static boolean isUnixInstaller()
```

Returns whether the installer is a Unix installer


**Returns:** the result

### isMacosInstaller()

```java
public static boolean isMacosInstaller()
```

Returns whether the installer is a macOS installer


**Returns:** the result

### getStandardApplicationsDirectory()

```java
public static String getStandardApplicationsDirectory()
```

Returns the standard directory for installing applications.


**Returns:** the directory

### isArchive()

```java
public static boolean isArchive()
```

Returns whether the application was installed from an archive or an installer media type.


**Returns:** `true` if this is an archive

### showUrl(URL)

```java
public static void showUrl(URL url)
```

Show a URL in the default browser. If the URL cannot be shown in a
browser, an error message is displayed.

For quiet installers, this method does nothing.


**Parameters:**

- `url` — the URL to be shown in the browser.

### showMessage(String, int)

```java
public static void showMessage(String message, int messageType)
```

Show a message dialog. This message dialog is a JOptionPane, the parameters are
equivalent to those of the standard Java class. This method works in both GUI and console mode.


**Parameters:**

- `message` — the message
- `messageType` — the message type (see JOptionPane)

**See also:**

- `JOptionPane`

### showMessage(String)

```java
public static void showMessage(String message)
```

Show a message dialog. This message dialog is a JOptionPane with a message type of INFORMATION_MESSAGE.
This method works in both GUI and console mode.

Paragraphs can be created by double line feeds (\n\n), single line feeds will be ignored.
On Windows and macOS, the string is split into a title that is shown in a larger font and a message that
is shown in a regular font. If the message does not contain a line feed, the entire message is shown as a title.
If the message contains a line feed, the title is split from the beginning up to the first dot or the first line
feed. The remainder is shown as the regular message. If you do not want to display anything in the title font,
start the message with a single line break.

Text is wrapped automatically on word boundaries. On Windows, text that cannot be wrapped is split with an
ellipsis in the middle of the line to limit the maximum width of the message dialog. In some circumstances this
may not be desirable, for example, for displaying stack trace elements. You can prepend the message with a
zero character `\0` to prevent word wrapping and ellipsis insertion. On macOS, text is wrapped on word
boundaries and within words if the width of a single word is too large. This behavior of the native alert
cannot be changed.


**Parameters:**

- `message` — the message

**See also:**

- `JOptionPane`

### showErrorMessage(String)

```java
public static void showErrorMessage(String message)
```

Show a message dialog. This message dialog is a JOptionPane with a message type of ERROR_MESSAGE.
This method works in both GUI and console mode.

See [`Util.showMessage`](Util.md#showmessage-string-int) for a discussion on the message format.


**Parameters:**

- `message` — the message

**See also:**

- `JOptionPane`

### showWarningMessage(String)

```java
public static void showWarningMessage(String message)
```

Show a message dialog. This message dialog is a JOptionPane with a message type of WARNING_MESSAGE.
This method works in both GUI and console mode.

See [`Util.showMessage`](Util.md#showmessage-string-int) for a discussion on the message format.


**Parameters:**

- `message` — the message

**See also:**

- `JOptionPane`

### showOptionDialog(String, String[], int)

```java
public static int showOptionDialog(String message, String[] options, int messageType) throws UserCanceledException
```

Show an option dialog. This option dialog is a JOptionPane, the parameters are
equivalent to those of the standard Java class. This method works in both GUI and console mode.

See [`Util.showMessage`](Util.md#showmessage-string-int) for a discussion on the message format.


**Parameters:**

- `message` — the message
- `options` — the options. To explicitly set keys for answers in console mode, prefix the desired letter
with '&'. For example `new String[] {"&amp;Enter", "E&amp;xit"}`. If no explicit keys are set,
the first letters of the options are taken. Repeating letters are replaced with numeric indices.
- `messageType` — the message type (see JOptionPane)

**Returns:** the index of the selected option or -1 if the installer is running in unattended mode without alerts

**Throws:**

- `UserCanceledException` — if the user cancels the selection in console mode

**See also:**

- `JOptionPane`

### fatalError(Throwable)

```java
public static void fatalError(Throwable t)
```

Alias for [`Context.handleCriticalException`](context/Context.md#handlecriticalexception-throwable).

### getParentWindow()

```java
@Deprecated public static Window getParentWindow()
```

> **⚠ Deprecated.** use [`UiUtil.getParentWindow`](UiUtil.md#getparentwindow) instead

Get the window of the installer application.


**Deprecated.** use [`UiUtil.getParentWindow`](UiUtil.md#getparentwindow) instead

### logInfo(Object, String)

```java
public static void logInfo(Object source, String message)
```

Writes an info message to the installation log.


**Parameters:**

- `source` — the object that requests the log. Usually the bean that executes this code. Can be `null`, in this
case the currently executed action or - if no action is being executed - the currently displayed screen is used. A
case where you should explicitly set this parameter is in code that is executed from form components.
- `message` — the message to be logged.

### logError(Object, String)

```java
public static void logError(Object source, String message)
```

Writes an error message to the installation log.


**Parameters:**

- `source` — the object that requests the log. Usually the bean that executes this code. Can be `null`, in this
case the currently executed action or - if no action is being executed - the currently displayed screen is used. A
case where you should explicitly set this parameter is in code that is executed from form components.
- `message` — the message to be logged.

### log(Throwable)

```java
public static void log(Throwable t)
```

Writes a stack trace to the installation log.


**Parameters:**

- `t` — the throwable instance

### dumpVariables(Context)

```java
public static void dumpVariables(Context context)
```

Dump all defined installer variables to stderr.
To be able to see the output, you have to create a debug installer by selecting
the corresponding option in the Build step in the install4j GUI.


**Parameters:**

- `context` — the context

### isAdminUser()

```java
@Deprecated public static boolean isAdminUser()
```

> **⚠ Deprecated.** use [`Util.isAdminGroup`](Util.md#isadmingroup) and [`Util.hasFullAdminRights`](Util.md#hasfulladminrights) instead.

Checks whether the current user has administration privileges.
- On Windows this method returns `true` if the installer is running with administration privileges.
- On macOS this method returns `true` if the user is member of the "admin" group.
- On Unix this method returns `true` if the current user is called "root".


**Returns:** `true` or `false`.

**Deprecated.** use [`Util.isAdminGroup`](Util.md#isadmingroup) and [`Util.hasFullAdminRights`](Util.md#hasfulladminrights) instead.

### isAdminGroup()

```java
public static boolean isAdminGroup()
```

Checks whether the current user is from the administration group.
- On Windows this is the case if the user is a member of the "Administrators" group.
- On macOS this method returns `true` if the user is member of the "admin" group.
- On Unix this method returns `true` if the current user is called "root".


**Returns:** `true` or `false`.

### hasFullAdminRights()

```java
public static boolean hasFullAdminRights()
```

Checks whether the current process has full administration rights.
- On Windows this method returns `true` if the installer is running with elevated privileges.
- On macOS and Unix this method returns `true` if the current user is called "root".


**Returns:** `true` or `false`.

### hasAllUserInstallationRights()

```java
public static boolean hasAllUserInstallationRights()
```

Checks whether the current process has the necessary rights to perform an installation for all users.

This is the case if the installer has been started with elevated privileges or if a "Request privileges" action
started the elevated helper process. On macOS, it is already sufficient if the user is in the admin group.


**Returns:** `true` or `false`.

### getAnnotatedStackTrace(Throwable)

```java
public static String getAnnotatedStackTrace(Throwable t)
```

If a stack trace comes from scripts, it can be difficult to trace them to their origin by looking at the
stack trace alone. With this method, you can annotate the stack trace with the actual names of properties,
actions, screens or form components that contain the offending script code.


**Parameters:**

- `t` — the exception

**Returns:** the annotated stack trace

### printAnnotatedStackTrace(Throwable)

```java
public static void printAnnotatedStackTrace(Throwable t)
```

Prints the return value of [`Util.getAnnotatedStackTrace`](Util.md#getannotatedstacktrace-throwable) to `System.err`.
This can be used for debugging purposes.


**Parameters:**

- `t` — the exception

### showPath(String)

```java
public static void showPath(String path)
```

Show the specified directory or file. On Windows this will open an explorer window, and on Linux/Unix it will open a terminal.
On macOS it will open a finder window for regular files or directories.


**Parameters:**

- `path` — the directory that should be displayed

### isDirectoryWritable(File)

```java
public static boolean isDirectoryWritable(File directory)
```

Checks if a directory is writable with the currently available privileges.


**Parameters:**

- `directory` — the directory. If the directory is a file, `false` is returned. It is acceptable if the
directory does not exist.

**Returns:** `true` or `false`.

### getLogFile()

```java
public static File getLogFile()
```

Get the location of the log file.
This is a temporary file, for the installer it will be moved to `.install4j/installation.log`
before the installer terminates if an "Install files" action has been executed. For an installer application,
this log file will be deleted when the installer application is terminated unless the VM parameter
`-Dinstall4j.keepLog=true` is set.


**Returns:** the log file

### moveLogFile(File)

```java
public static void moveLogFile(File newLogFile)
```

Move the log file to a new location.
With this method, an alternative permanent location of the log file can be specified. The log file
will be moved immediately, and it will not be deleted when the installer application exits.


**See also:**

- [Util.getLogFile](Util.md#getlogfile)

### loadNativeFromResources(String)

```java
public static void loadNativeFromResources(String fileName)
```

Call `System#load` with the supplied file name resolved against the resource directory.
This is useful to load supporting native libraries for SQL drivers, such as
`sqljdbc_auth.dll` for the Microsoft JDBC driver for SQL Server. If you add that DLL on the
"Installer->Custom Code & Resources" step, you can load it with the code

```
    Util.loadNativeFromResources("sqljdbc_auth.dll");
```

in a "Run script" action.


**Parameters:**

- `fileName` — the file name of the native library

## Constructors

| Signature | Description |
|---|---|
| [`Util()`](#util) |  |

### Util()

```java
public Util()
```

