Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Maven classpath to run Java main class?

Tags:

java

maven

rhino

I'm currently using Maven to build my Rhino JavaScript project, download dependent libraries, and manage the classpath at runtime. I'm able to run the JavaScript entry point by using the Maven exec plugin, in the following way:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>org.mozilla.javascript.tools.shell.Main</mainClass>
                <classpathScope>runtime</classpathScope>
                <arguments>
                    <argument>path/to/entryPoint.js</argument>
                </arguments>
            </configuration>
        </plugin>

This works well, but the problem is that maven takes about 10 seconds just to start, which is about 10 times longer than it takes my program to run. Is there a way to either:

  1. improve the performance of the maven exec plugin so that it takes less time to start, or
  2. export the classpath that maven would use at runtime, so that I can just start my program from a script?
like image 729
jbeard4 Avatar asked Jun 07 '11 02:06

jbeard4


People also ask

How do I run a main class in a Maven project?

The exec-maven-plugin And we want to execute its main method from the command line via Maven. In order to do this, we can use the exec-maven-plugin. To be more specific, the exec:java goal from this plugin executes the supplied Java class with the enclosing project's dependencies as the classpath.

How do I run a particular class in Maven?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.


1 Answers

  1. You can use the -o / --offline switch to tell Maven to not bother checking for snapshot or plugin updates.

  2. Use the appassembler or assembly plugins to generate startup scripts which will automatically (in the case of appassembler) reference the desired classpath.

like image 80
matt b Avatar answered Oct 20 '22 20:10

matt b