Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven exec:java run class file within jar

Tags:

maven

I have my code packaged into a jar

The jar is packaged ok.

jar -tfv target/test-1.0-SNAPSHOT.jar

com/
com/codevalid/
com/codevalid/App.class
log4j.xml
META-INF/maven/com.codevalid/test/pom.xml
META-INF/maven/com.codevalid/test/pom.properties

I can execute them when they are present as individual class files using exec:java

How to run class file within jar using maven exec:java?

like image 675
codevalid Avatar asked Mar 29 '12 12:03

codevalid


People also ask

How do I run a Java class in a jar classpath?

We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …] As we can see, in this case, we'll have to include the main class name in the command line, followed by arguments.

What does Mvn exec java?

mvn exec:java is a goal from the exec plugin for maven. It lets you specify a main class to execute (see pom. xml). This lets you avoid having to figure out the proper java command to run and classpath arguments and the like.


2 Answers

Ok, this is what i finally ended up doing.
I built the jar using

mvn assembly:single

and used

java -jar ./target/App-1.0-SNAPSHOT-jar-with-dependencies.jar com.codevalid.App

I did see an alternative where i could have used

mvn exec:java -Dexec.mainClass="com.codevalid.App"

But i was not sure how pass the name of the jar as a classpath

like image 164
codevalid Avatar answered Jan 03 '23 06:01

codevalid


You can run a jar file using the exec:java goal by adding some arguments:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <mainClass>org.example.Main</mainClass>
        <arguments>
            <argument>-jar</argument>
            <argument>target/myJar-1.0-SNAPSHOT.jar</argument>
        </arguments>
    </configuration>
</plugin>

If you have an executable jar and don't want to define the entry point, you need to set the executable and use the exec:exec goal:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-jar</argument>
            <argument>target/myJar-1.0-SNAPSHOT.jar</argument>
        </arguments>
    </configuration>
</plugin>
like image 25
kapex Avatar answered Jan 03 '23 05:01

kapex