maven-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Eggers <its_toas...@yahoo.com>
Subject Re: Build is successful but can't run?
Date Wed, 05 Feb 2014 19:00:08 GMT
On 2/5/2014 9:52 AM, TenLeftFingers wrote:
> When I build my Maven project with dependencies (using Netbeans but pointing
> to a vanilla Maven installation), I get BUILD SUCCESS. Great!
>
> When I try to /run /it in the form of *java -cp
> target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App* I get this:
> *java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory*
>
> How come this dependency fails at this late stage?
>
> Trying to run the class from NetBeans itself gives a different error:
> *Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec*
>
> Please enlighten me as to how I can run this thing. It works fine in Eclipse
> when I run it as a Java Application - but Eclipse has other problems that
> I'm avoiding so I need to do this either from CLI or NetBeans (preferable).
>
> Thanks,
> Ten

I normally use NetBeans . . . .

One way to handle this (and it'll end up looking like a standard 
NetBeans project) is the following.

1. Modify the manifest via the maven-jar-plugin

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <version>2.4</version>
   <configuration>
     <archive>
       <manifest
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
         <mainClass>main-class-name</mainClass>
         <addClasspath>true</addClasspath>
         <classpathPrefix>lib/</classpathPrefix>
       </manifest>
     </archive>
   </configuration>
</plugin>

2. Use the maven-dependency-plugin to copy over the libraries

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
   <version>2.8</version>
   <executions>
     <execution>
       <phase>package</phase>
       <goals>
         <goal>copy-dependencies</goal>
       </goals>
       <configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
         <includeScope>compile</includeScope>
       </configuration>
     </execution>
   </executions>
</plugin>

3. Run from the command line to test

mvn package
cd target
java -jar jarFileName.jar

To ship the binaries, use the maven-assembly-plugin and package both the 
JAR and the supporting libraries. That way when someone unpacks the zip 
(or tar or tar.gz, etc.), the directory structure will be maintained.

Here's an assembly.xml file. I've left out the xml namespace declarations:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
     <id>bin</id>
     <formats>
         <format>tar.gz</format>
         <format>zip</format>
     </formats>
     <includeBaseDirectory>false</includeBaseDirectory>
     <includeSiteDirectory>false</includeSiteDirectory>
     <fileSets>
         <fileSet>
             <directory>target</directory>
             <outputDirectory></outputDirectory>
             <includes>
                 <include>*.jar</include>
                 <include>lib/</include>
             </includes>
         </fileSet>
     </fileSets>
</assembly>

Other ways include creating an uber-jar.

. . . . just my two cents.
/mde/


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Mime
View raw message