Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8342641

Launcher allows other flags between -jar and jarfile

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8
    • tools
    • None

      The java launcher allows other flags to appear between -jar and its argument.

      The following example shows that `java -jar hello.jar` and `java -jar -Xmx2g hello.jar` have equivalent behaviour, where in the second example the launcher scans past other arguments to find the jar file argument to `-jar`.

      https://docs.oracle.com/en/java/javase/23/docs/specs/man/java.html suggests that the jarfile should immediately follow -jar:

      > java [options] -jar jarfile [args ...]
      ...
      > -jar jarfile
      > Executes a program encapsulated in a JAR file. The jarfile argument is the name of a JAR file with a manifest that contains a line in the form Main-Class:classname that defines the class with the public static void main(String[] args) method that serves as your application's starting point. When you use -jar, the specified JAR file is the source of all user classes, and other class path settings are ignored. If you're using JAR files, then see jar.

      $ java -fullversion
      openjdk full version "24-ea+15-1658"

      $ cat Hello.java
      class Hello {
        public static void main(String[] args) {
          System.err.println("hello world");
        }
      }
      $ javac Hello.java
      $ jar cvfe hello.jar Hello Hello.class

      $ java -jar hello.jar
      hello world
      $ java -jar -Xmx2g hello.jar
      hello world

            jpai Jaikiran Pai
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: