Search code examples
javamavencommand-linejarapache-storm

Override main class specified in pom.xml while running jar from command line


I have 2 classes with main methods in a maven project.(NOT SPRING BOOT) class A and class B

class A{
    public static void main(String[] args){ 
        System.out.println("CLASS A");
   }
}

class B{
    public static void main(String[] args){ 
        System.out.println("CLASS B");
   }
}

And I have specified the default main class as following in pom:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <transformers>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.example.A</mainClass>
                    </transformer>
                </transformers>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Now when I run the project using java -jar from command line, the main method A executed as expected. The command I use is as follows:

java -jar myExample-Snapshot.jar

It runs the class A as expected since I have specified it under <mainClass>com.example.A</mainClass> in my pom.xml.

Now I want to specify class A or class B when I execute my jar from command line. I tried the following ,

java -jar myExample-Snapshot.jar com.example.B

But it ran class A(specified in pom) with com.example.B as command line argument(stored in String[] args)

Then I tried adding main class B to my pom as shown below:

<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.example.A</mainClass>
                        <mainClass>com.example.B</mainClass> 
                    </transformer>

command: java -jar myExample-Snapshot.jar com.example.A

But this time it ran main from class B and completely ignored class A.(Again, com.example.A was taken as command line argument)

Is it doable? or should I take a different approach.?


Solution

  • Starting java with the -jar parameter will cause java to look into the manifest.mf file (contained in the jar) where the main class to be executed is indicated. Obviously your maven setup creates this manifest.mf accordingly for you.

    Starting java with -cp is only indicating in which jar(s) java should look up needed classes. The first non qualified parameter on the start line indicates the main class whose main() method is executed. All further parameters are passed to the main method as args[].