---
title: "Intercept external commands"
source: "https://cheerpj.com/docs/guides/Intercept-external-commands"
---

# Intercept external commands

How to intercept external system commands that are executed in a Java application

Java programs run external system commands with methods such as `Runtime.getRuntime().exec(command)` or `new ProcessBuilder(command)`. For example, a Java application might run a command to open a URL in the default browser, launch a script, or run a platform-specific utility. On the desktop, these commands start processes on the operating system.

In the browser, CheerpJ cannot start operating system processes. With the [`execCallback`](https://cheerpj.com/docs/reference/cheerpjInit.md#execcallback) option of [`cheerpjInit`](https://cheerpj.com/docs/reference/cheerpjInit.md), CheerpJ passes these commands to a JavaScript function instead, so you can implement the same behaviour in JavaScript. Without it, the command is rejected.

## execCallback option

The [`execCallback`](https://cheerpj.com/docs/reference/cheerpjInit.md#execcallback) option takes a function, which CheerpJ calls whenever the Java application runs an external command. CheerpJ passes the details of the command to the function, so it can implement the command’s behaviour in JavaScript.

The function receives two parameters:

- `cmdPath`: the command that Java tried to run.
- `argsArray`: an array of the arguments passed to the command.

## Example walkthrough

This Java program opens a URL:

`Example.java`

```java
import java.io.IOException;


public class Example
{
  public static void main(String[] a) throws IOException, InterruptedException
  {
    String url = "http://www.example.com";
    // The xdg-open command opens a URL in the default browser on Linux
    String command = "xdg-open " + url;
    Runtime.getRuntime().exec(command);
  }
}
```

The program uses `Runtime.getRuntime().exec(command)` to open a URL in the default web browser on Linux, with the `xdg-open` command. On Windows, the equivalent command is `cmd /c start`.

The same behaviour can be implemented in JavaScript:

`index.html`

```js
function execCb(cmdPath, argsArray) {
  // Check for the xdg-open command
  if (cmdPath == "xdg-open") {
    // Open the URL in a new browser tab
    // argsArray[0] is "http://www.example.com" in this example
    window.open(argsArray[0], "_blank");
  }
}
```

The function checks whether the command is `xdg-open`. If it is, it opens the URL in a new browser tab with `window.open()`.

Pass this function to [`cheerpjInit`](https://cheerpj.com/docs/reference/cheerpjInit.md) with the [`execCallback`](https://cheerpj.com/docs/reference/cheerpjInit.md#execcallback) option, so that CheerpJ calls it whenever the Java application runs an external command:

`index.html`

```js
function execCb(cmdPath, argsArray) {
  if (cmdPath == "xdg-open") {
    window.open(argsArray[0], "_blank");
  }
}


(async function () {
  await cheerpjInit({ execCallback: execCb });
  await cheerpjRunMain("Example", "/app/");
})();
```

The `Runtime.getRuntime().exec(command)` call in the Java application opens a new browser tab, implemented in JavaScript.
