Skip to content

JBANG_JAVA_OPTIONS are not restricted to JBang onlyΒ #2341

@wfouche

Description

@wfouche

Options set in JBANG_JAVA_OPTIONS are passed to the JVM that JBang creates to run a particular Java/Kotlin/Groovy script.

bash-3.2$ export JBANG_JAVA_OPTIONS=-XX:TieredStopAtLevel=1
bash-3.2$ jbang run JvmRuntimeOpts.java 1 2 3
--- πŸš€ JVM Runtime Options (VM Arguments) ---
Option 1: -XX:TieredStopAtLevel=1

--- πŸ“ Application Arguments ---
Argument 1: 1
Argument 2: 2
Argument 3: 3
import java.lang.management.ManagementFactory;
import java.util.List;

/**
 * JvmRuntimeOpts.java
 * This program demonstrates how to retrieve and display:
 * 1. JVM Runtime Options (VM arguments).
 * 2. Application Arguments (arguments passed to the main method).
 */
public class JvmRuntimeOpts {

    public static void main(String[] args) {
        // --- 1. Display JVM Runtime Options ---
        System.out.println("--- πŸš€ JVM Runtime Options (VM Arguments) ---");

        // Use the ManagementFactory to get the RuntimeMXBean
        List<String> jvmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();

        if (jvmArgs.isEmpty()) {
            System.out.println("No explicit JVM Runtime Options found (default settings are in use).");
        } else {
            for (int i = 0; i < jvmArgs.size(); i++) {
                System.out.println("Option " + (i + 1) + ": " + jvmArgs.get(i));
            }
        }

        System.out.println("\n" + "--- πŸ“ Application Arguments ---");

        // --- 2. Display Application Arguments ---
        if (args.length == 0) {
            System.out.println("No Application Arguments were passed to the main method.");
        } else {
            for (int i = 0; i < args.length; i++) {
                System.out.println("Argument " + (i + 1) + ": " + args[i]);
            }
        }

        System.out.println("\n" + "--- βœ… Execution Complete ---");
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions