简体   繁体   中英

How to run a compiled java project

I am new to maven. In order to compile my project I hit mvn compile. However I can't figure out how I can run my project from inside the maven. For instance when I hit

mvn exec:java -Dexec.mainClass="main.java.org.dbalancer.StartProgram"

maven complaints:

java.lang.NoClassDefFoundError: com/sanityinc/jargs/CmdLineParser$OptionException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: com.sanityinc.jargs.CmdLineParser$OptionException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 6 more

which means that jargs library is not included in classpath. However in my project there aren't any libraries included under target folder which means any library I use I should download it on my own and included in classpath (or use .m2/ folder). For instance, when I am under target/classes folder, and I have downloaded all the necessary libraries this command works: java -cp .:../../lib/jargs-2.0-SNAPSHOT.jar:../../../dom4j-2.0.0-ALPHA-2.jar:../../../log4j-1.2.17.jar main.java.org.dbalancer.StartProgram

However isn't that a little verbose? How can I run that from inside maven? Can I run it using java command but not downloading on my own the necessary libraries ?

Update : Ok the problem was a library that I included in that way:

<dependency>
  <groupId>jargs_local</groupId>
  <artifactId>jargs</artifactId>
  <version>2.0-SNAPSHOT</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/jargs-2.0-SNAPSHOT.jar</systemPath>
</dependency>

Now jargs where moved to official maven repo. However does anyone know how can I speed up mvn exec? It's a bit slow if you just want to check something..

The maven-exec plugin should set up the classpath for you. Are you perhaps missing jargs as dependency in your pom.xml (can we see that as well as the complete mvn dump?)

Cheers,

One simple way to build a self-sufficent runnable jar is to build a jar-with-dependencies with the maven assembly plugin.

http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html

you should dependency them int the pom.xml for example:

  <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.0</version>
     </dependency>

or

<build>
        <plugins>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                  <source>1.6</source>
                  <target>1.6</target>
                  <encoding>UTF-8</encoding>
                  <compilerArguments>
                   <extdirs>src\main\webapp\WEB-INF\lib</extdirs>
                 </compilerArguments>
              </configuration>
            </plugin>
        </plugins>
    </build>

or

<dependency>
    <groupId>org.swinglabs</groupId>
    <artifactId>swingx</artifactId>
    <version>0.9.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/swingx-0.9.3.jar</systemPath>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM