# Java and the JVM

> Make Java, Gradle and Android Studio trust Cloak Proxy's certificate and send their traffic through it.

Source: https://docs.subcloak.com/cloak-proxy/devices/java-firefox-docker-flutter-electron/

Java keeps its own list of trusted certificates and its own proxy settings, so the macOS proxy and keychain don't reach it. The **Java & JVM** page of **Device & Runtime Setup** has both commands, filled in with your port and your certificate's path, each with a **Copy** button.

*[Screenshot: The Java & JVM page with the keytool command to trust the certificate and the JVM proxy flags, each with a Copy button. The Java & JVM page.]*

Firefox, Docker, Flutter and Electron have their own pages now: [Capturing browser and Electron apps](https://docs.subcloak.com/cloak-proxy/devices/capturing-browser/), [Docker](https://docs.subcloak.com/cloak-proxy/devices/docker/) and [Flutter](https://docs.subcloak.com/cloak-proxy/devices/flutter/).

In the commands, the certificate path is written as `$HOME/Library/Application Support/CloakProxy/cloak-proxy-ca.pem` so you can paste it into a shell. The port is the one Cloak Proxy listens on (`9090` by default).

## Java (JVM, Gradle, Android Studio)

Java uses its own trust store, `cacerts`, and its own proxy settings.

1. Import the Cloak Proxy certificate into the JDK's trust store. Copy the command from the **Java (JVM, Gradle, Android Studio)** section and run it:

   ```bash title="Terminal"
   sudo keytool -importcert -noprompt -alias cloak-proxy -cacerts -storepass changeit -file "$HOME/Library/Application Support/CloakProxy/cloak-proxy-ca.pem"
   ```

   `changeit` is the JDK's default trust store password. `-cacerts` needs Java 9 or later.

2. Run your program with the proxy settings:

   ```bash title="Terminal"
   java -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=9090 -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=9090 -jar app.jar
   ```

`keytool` changes the JDK it belongs to. If you use more than one JDK (Android Studio bundles its own), run the `keytool` of each JDK that should trust the certificate. A JDK update can replace `cacerts`, so import again after updating.

To pass the proxy settings without editing a command line, set them in the environment or, for Gradle, in `gradle.properties`:

**Any Java program**

```bash title="Terminal"
export JAVA_TOOL_OPTIONS="-Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=9090 -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=9090"
```

**Gradle**

```properties title="gradle.properties"
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=9090
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=9090
```

**Undo:** remove the certificate and the proxy settings.

```bash title="Terminal"
sudo keytool -delete -alias cloak-proxy -cacerts -storepass changeit
```

## Firefox

The easiest way is a [capturing Firefox](https://docs.subcloak.com/cloak-proxy/devices/capturing-browser/): its own profile, already proxied. To use your everyday Firefox instead, trust the certificate on this Mac (**Settings › Certificate**), then in `about:config` set `security.enterprise_roots.enabled` to `true` and turn on macOS capture. Firefox follows the macOS proxy by default.

## Related

  - [Terminal and runtimes](https://docs.subcloak.com/cloak-proxy/devices/terminal-and-runtimes/): Node.js, Python, Ruby, Go, curl, git, Deno and AWS CLI.
  - [Apps that ignore the proxy](https://docs.subcloak.com/cloak-proxy/capture/apps-that-ignore-the-proxy/): Capture apps that skip proxy settings entirely.
  - [Certificate](https://docs.subcloak.com/cloak-proxy/capture/certificate/): Install and trust the root certificate on this Mac.
  - [Other devices and access control](https://docs.subcloak.com/cloak-proxy/devices/other-devices-and-access-control/): Allow containers and devices to connect safely.
  - [Capturing browser](https://docs.subcloak.com/cloak-proxy/devices/capturing-browser/): A Chrome or Firefox that's already proxied, and Electron apps.
  - [Docker](https://docs.subcloak.com/cloak-proxy/devices/docker/): Capture a Compose project's containers.
  - [Flutter](https://docs.subcloak.com/cloak-proxy/devices/flutter/): A debug-only proxy file for Flutter apps.
