anothernode
anothernode

Reputation: 5397

How to enable Java assertions in Visual Studio Code

When running a Java program from the command line, assertions can be enabled with the -enableassertions option for the java command. Running this program would then (and only then) fail with an AssertionError:

public class App {
    public static void main(String[] args) throws Exception {
        foo(2);
    }

    private static void foo(int x) {
        assert x > 5;
        System.out.println(x);
    }
}

How can this be done when running a Java program in Visual Studio Code with the Java Extension Pack?

Upvotes: 13

Views: 8237

Answers (1)

anothernode
anothernode

Reputation: 5397

Visual Studio Code manages launch configurations in the launch.json file in the project folder root.

The -enableassertions option can be added there with the vmArgs key like this:

{
    "configurations": [
        {
            "type": "java",
            "name": "My App",
            "request": "launch",
            "mainClass": "App",
            "projectName": "my-app",
            "vmArgs": "-enableassertions"
        }
    ]
}

Upvotes: 23

Related Questions