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 20:05:33 GMT
On 2/5/2014 11:40 AM, TenLeftFingers wrote:
> Hi Mark,
>
> Thanks for that. I changed the pom as you suggested. When I ran with java
> -jar myApp.jar I got the output:
> *Failed to load Main-Class manifest attribute from target/myApp.jar*
>

A couple of things:

1. the main-class-name was supposed to be your main class

> I opened the manifest and the *Main-Class* attribute was entered as
> *mainClass.* So I changed it to the former and that got rid of that error.
> But now  I *still *get the original error.
>
> The only pom change I'm not sure about is
> *<classpathPrefix>lib/</classpathPrefix>*. What classes is that classpath
> trying to capture?
>

You need to change to the project target directory in order for it to 
run and find the supporting jars.

The classpathPrefix line sets where to look for additional JAR files. It 
is relative to where you are.

Maven compiles and packages your program into a JAR. It copies all of 
the supporting JAR files listed in your dependencies into target/lib.

Before you run, make sure that target/lib exists and has all of the JAR 
files you need.

Now, change to the target directory on the command line, and execute the 
program. The keys here are the following:

1. change to the target directory on the command line
2. execute the program with the following:

java -jar my-app-1.0-SNAPSHOT.jar

Now the lib directory will be in the correct relative path, and your 
dependencies should be found.

> Thanks,
> Ten
>

The exec plugin (http://mojo.codehaus.org/exec-maven-plugin/) as Wayne 
has mentioned is another way to accomplish this. This will help you run 
from within an IDE or by using mvn exec:exec, but it won't help you 
package and ship your code out for other users.

You can combine both. I've not used the exec-maven-plugin, so creating 
the plugin configuration for that is left as an exercise for the reader. 
However, the documentation on the link listed above should get you started.

/mde/

>
> On Wed, Feb 5, 2014 at 7:01 PM, Mark Eggers [via Maven] <
> ml-node+s40175n5783298h15@n5.nabble.com> wrote:
>
>> 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