# Variables

Import: `import com.install4j.api.launcher.Variables;`

```java
public class Variables
```

This class provides methods to access compiler and installer variables from your launchers.
Do not use this class in the installer, uninstaller or in a custom installer application, in that case use
the methods in the [`context`](../context/Context.md) instead.

## Methods

| Signature | Returns | Description |
|---|---|---|
| [`getCompilerVariable(String)`](#getcompilervariable-string) | `String` | Get the value of a compiler variable. |
| [`getInstallerVariables()`](#getinstallervariables) | `Map<String, Object>` | Get a map of installer variables that were saved to the automatically created response file `response.varfile`. |
| [`getInstallerVariable(String)`](#getinstallervariable-string) | `Object` | Get the value of an installer variable that was saved to the automatically created response file `response.varfile`. |
| [`loadFromPreferenceStore(boolean)`](#loadfrompreferencestore-boolean) | `Map<String, Object>` | Load installer variables from the preference stores that have been saved by a "Save installer variables to the preference store" action. |
| [`loadFromPreferenceStore(String, boolean)`](#loadfrompreferencestore-string-boolean) | `Map<String, Object>` | Load installer variables from the preference stores that have been saved by a "Save installer variables to the preference store" action. |
| [`saveToPreferenceStore(Map<String, Object>, boolean)`](#savetopreferencestore-map-string-object-boolean) | `void` | Save a map of installer variables to the preference store. |
| [`saveToPreferenceStore(Map<String, Object>, String, boolean)`](#savetopreferencestore-map-string-object-string-boolean) | `void` | Save a map of installer variables to the preference store. |
| [`clearInstallerVariablesCache()`](#clearinstallervariablescache) | `void` | Clear the cache of read installer variables. |

### getCompilerVariable(String)

```java
public static String getCompilerVariable(String variableName) throws IOException
```

Get the value of a compiler variable. The first time this method is called, the installer config file will be read.
For subsequent calls the compiler variables will be cached.


**Parameters:**

- `variableName` — the name of the compiler variable

**Returns:** the value of the compiler variable or `null` if the variable is undefined

**Throws:**

- `IOException` — if the installer config file cannot be read

### getInstallerVariables()

```java
public static synchronized Map<String, Object> getInstallerVariables()
```

Get a map of installer variables that were saved to the automatically created response file `response.varfile`.
Not all installer variables are saved to that file. You can register a variable as a response file variable
with [`Context.registerResponseFileVariable`](../context/Context.md#registerresponsefilevariable-string). Variables to which
form components are bound are automatically registered as response file variables.

If the automatically created response file cannot be read, only system installer variables are available.


**Returns:** the map with installer variables from the response file. The keys are the variable names, the values are
the original values at the time that the response file was saved.

### getInstallerVariable(String)

```java
public static synchronized Object getInstallerVariable(String variableName)
```

Get the value of an installer variable that was saved to the automatically created response file `response.varfile`.
See [`Variables.getInstallerVariables`](Variables.md#getinstallervariables) for more information.

If the automatically created response file cannot be read, only system installer variables are available.


**Parameters:**

- `variableName` — the name of the compiler variable

**Returns:** the value of the compiler variable or `null` if the variable is undefined

**See also:**

- [Variables.getInstallerVariables](Variables.md#getinstallervariables)

### loadFromPreferenceStore(boolean)

```java
public static Map<String, Object> loadFromPreferenceStore(boolean userSpecific) throws IOException
```

Load installer variables from the preference stores that have been saved by a "Save installer variables to the preference store" action.
The package name in the preference store is set to the application ID, which is the default save location of that action.


**Parameters:**

- `userSpecific` — if the user-specific preference store should be used or not

**Returns:** the map with installer variables from the preference store. The keys are the variable names, the values are
the original values at the time that the installer variables were saved. Returns `null` if no
variables were saved.

**Throws:**

- `IOException` — if the operation fails due to a problem with the backing store

### loadFromPreferenceStore(String, boolean)

```java
public static Map<String, Object> loadFromPreferenceStore(String packageName, boolean userSpecific) throws IOException
```

Load installer variables from the preference stores that have been saved by a "Save installer variables to the preference store" action.


**Parameters:**

- `packageName` — the package name that was used when saving the installer variables to the preference store
- `userSpecific` — if the user-specific preference store should be used or not

**Returns:** the map with installer variables from the preference store. The keys are the variable names, the values are
the original values at the time that the installer variables were saved. Returns `null` if no
variables were saved.

**Throws:**

- `IOException` — if the operation fails due to a problem with the backing store

### saveToPreferenceStore(Map<String, Object>, boolean)

```java
public static void saveToPreferenceStore(Map<String, Object> variables, boolean userSpecific) throws IOException
```

Save a map of installer variables to the preference store. If you use installer variables loaded by
[`Variables.loadFromPreferenceStore`](Variables.md#loadfrompreferencestore-boolean) for the configuration of your application,
you can save changes made in the application back to the preference store with this method. Values that cannot be
decoded as described by [`Context.registerResponseFileVariable`](../context/Context.md#registerresponsefilevariable-string) will be ignored.
Values that are present in the preference store and not in the supplied map will not be deleted.
The package name in the preference store is set to the application ID, which is the default save location of the
"Save installer variables to the preference store" action.


**Parameters:**

- `variables` — the map with the installer variables as returned by [`Variables.loadFromPreferenceStore`](Variables.md#loadfrompreferencestore-boolean)
- `userSpecific` — if the user-specific preference store should be used or not

**Throws:**

- `IOException` — if the operation fails due to a problem with the backing store

### saveToPreferenceStore(Map<String, Object>, String, boolean)

```java
public static void saveToPreferenceStore(Map<String, Object> variables, String packageName, boolean userSpecific) throws IOException
```

Save a map of installer variables to the preference store. If you use installer variables loaded by
[`Variables.loadFromPreferenceStore`](Variables.md#loadfrompreferencestore-string-boolean) for the configuration of your application,
you can save changes made in the application back to the preference store with this method. Values that cannot be
decoded as described by [`Context.registerResponseFileVariable`](../context/Context.md#registerresponsefilevariable-string) will be ignored.
Values that are present in the preference store and not in the supplied map will not be deleted.


**Parameters:**

- `variables` — the map with the installer variables as returned by [`Variables.loadFromPreferenceStore`](Variables.md#loadfrompreferencestore-string-boolean)
- `packageName` — the package name to which the installer variables should be saved in the preference store
- `userSpecific` — if the user-specific preference store should be used or not

**Throws:**

- `IOException` — if the operation fails due to a problem with the backing store

### clearInstallerVariablesCache()

```java
public static synchronized void clearInstallerVariablesCache()
```

Clear the cache of read installer variables. If you know that the response file has changed, you can call this method
to force the installer variables to be read again. The methods in
[`ApplicationLauncher`](ApplicationLauncher.md) call this method automatically when the custom installer
application exits.

