8

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?

radoh
  • 4,554
  • 5
  • 30
  • 45
codevalid
  • 191
  • 1
  • 3
  • 8

4 Answers4

7

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

Leif Gruenwoldt
  • 13,561
  • 5
  • 60
  • 64
codevalid
  • 191
  • 1
  • 3
  • 8
4

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>
kapex
  • 28,903
  • 6
  • 107
  • 121
  • What if I want to pass some arguments to the main method of the `org.example.Main` class ? – prime Feb 20 '17 at 06:13
  • 1
    @prime I 'm haven't tested it but I think you can use [``](http://www.mojohaus.org/exec-maven-plugin/exec-mojo.html#commandlineArgs) to pass arguments to the actual application – kapex Feb 20 '17 at 12:36
  • Thanks for the suggestion, will check that. Anyway got an answer to the actual problem http://stackoverflow.com/questions/42337857/mvn-execjava-to-run-a-java-file-in-an-external-jar-file – prime Feb 20 '17 at 12:41
3

You need to include your jar file as a dependency to the exec plugin, e.g. like this:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
  <execution>
    <phase>install</phase>
    <goals>
      <goal>java</goal>
    </goals>
    <configuration>
      <mainClass>com.codevalid.App</mainClass>
    </configuration>
  </execution>
</executions>
<dependencies>
  <dependency>
    <groupId>myGroup</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>

You can skip the dependency declaration if the com.codevalid.App class is compiled as part of your current project.

nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • 1
    Hi, Thanks for the info, but this seems to work only if the jar in the repository. What i wanted to do is to test the jar, before i can install it in the repo. Is there any way where I can specify the **location of this jar** to maven exec:java – codevalid Mar 29 '12 at 16:52
  • @codevalid, @nwinkler's answer should be sufficient for your purposes because running `mvn install` on your dependent project in your local environment will put it in your local .m2 repository. – user2910265 Aug 12 '19 at 19:37
1

You have to specify classpathScope and includePluginDependencies or includeProjectDependencies parameters to pickup jar files on the classpath.

Here is an example:

           <configuration>
                <executable>java</executable>
                <mainClass>com.google.jstestdriver.JsTestDriver</mainClass>
                <classpathScope>test</classpathScope>
                <includePluginDependencies>true</includePluginDependencies>
                <includeProjectDependencies>true</includeProjectDependencies>
                <commandlineArgs>--port 9876</commandlineArgs>
            </configuration>
dbrin
  • 15,525
  • 4
  • 56
  • 83