4

I'm trying to run a java program using the maven exec plugin using the exec:exec goal.
I need to add an additional jar to the classpath (the sun tools jar).
Since the includePluginDependencies works only for the exec:java goal I thought adding it manually in the arguments section but couldn't find a way to concatenate it to the base classpath. The problem is that since the jar is defined as system scope, maven won't add it to the run-time classpath and I need to add it manually.
If someone knows how to do so from the command line it's even better. Thanks in advance,
Avner

  • You can see the plugin section bellow

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <dependencies>
                <dependency>
                    <groupId>com.sun</groupId>
                    <artifactId>tools</artifactId>
                    <scope>system</scope>
                    <systemPath>${JDK_HOME}/lib/tools.jar</systemPath>
                </dependency>
                <dependency>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>myArtifact</artifactId>
                    <version>1.0</version>
                </dependency>
            </dependencies>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-classpath</argument>
                    <classpath/>                        
                    <argument>com.mycompany.MyMainClass</argument>
                </arguments>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    
Avner Levy
  • 6,601
  • 9
  • 53
  • 92
  • I've had to solve almost this exact problem, but my configuration looked much like yours (``). I'm unclear what you mean by "concatenate it to the base classpath." Do you mean the "Boot" classpath? Can you please provide a snippet of your build output and describe your end goal? – noahlz Sep 07 '12 at 18:56
  • My goal was to run my java program with the run-time classpath calculated by maven while adding to it the tools.jar. Since the tools is defined as system scope it isn't added to the run-time classpath calculated by maven. Eventually I decided to use the maven-antrun-plugin. – Avner Levy Sep 09 '12 at 07:11

3 Answers3

2

Eventually I've decided to use the maven-antrun-plugin so here is a possible alternative solution.

<configuration>
<target>
    <property name="runtime_classpath" refid="maven.runtime.classpath"/>

    <java classname="com.mycompany.MyClass"
            fork="true"
            spawn="false"
            failonerror="true"
            maxmemory="512m" >

        <classpath>
            <pathelement path="${runtime_classpath}" />
            <pathelement path="${JDK_HOME}/lib/tools.jar" />
        </classpath>
        <arg value="${ant.param1}" />
        <arg value="${ant.param2}" />
        <arg value="${ant.param3}" />
        <arg value="${ant.param4}" />
        <arg value="${ant.param5}" />
    </java>
</target>
</configuration>
Avner Levy
  • 6,601
  • 9
  • 53
  • 92
  • I still don't understand how this is different from the original question or what problem you are actually trying to solve. – noahlz Sep 09 '12 at 11:48
  • As you can see above I need to run a java class which has a large dependency list which is calculated by maven (the runtime classpath). What I'm looking for is a way to add a jar (tools.jar) to the calculated classpath maven built and use it for running my java class. In the example above you can see that in the antrun plugin you can concatenate the calculated maven runtime classpath with additional jars. If you have a simple way doing so with the maven exec plugin feel free to share. – Avner Levy Sep 12 '12 at 16:03
  • Updated my answer to suggest using `-Dexec.classpathScope="system"` – noahlz Sep 12 '12 at 17:58
1

You can try setting the CLASSPATH environment variable.

gkamal
  • 20,777
  • 4
  • 60
  • 57
  • I think that the CLASSPATH variable is overridden by the -classpath argument given to the java executable. The OP needs that Maven-generated classpath to assemble project dependencies. I could give this a try anyway though. – Jesse Hallett Sep 06 '12 at 19:59
1

Try adding

<argument>-Xbootclasspath/a:${env.JAVA_HOME}/lib/tools.jar</argument>

From the command line, add

-Dexec.args="-Xbootclasspath/a:$JAVA_HOME/lib/tools.jar"

Another option is to declare the tools.jar as a System dependency and then set the exec plugin scope to "system." See: exec-maven-plugin - classpathScope

noahlz
  • 10,202
  • 7
  • 56
  • 75