1

I have seen this question posted here before and I have looked at the solutions however I cannot fix the problem I'm having. I created a very simple Maven project in Eclipse for Java and I want to run the output jar file e.g. java -jar jarfilename.jar

I can run the program by right clicking on the project in eclipse and indicating run as Java application. I can build the project to a jar file with mvn package. Running the jar file I get the output of NoClassDefFoundError for the joda time. The joda jar files are in the configured repository e.g. .m2/repository/joda-time/joda-time/2.8.2. There are no errors indicated for the project in Eclipse. I'm using jdk1.8.0_92 Maven version 3.3.9 and eclipse Java EE Neon release 4.6.0. Java home is configured in the environment variables and so too is the class path as: ...\Java\jdk1.8.0_92\jre\lib;C:\Users\username.m2\repository

Some additional information the classpath is correct in terms of not having typos in it. I also looked at a solution from another similar question wherein the suggestion was to add the external jar to the bootstrap entries under run configuratotion. I have also made an entry in the Java build path for joda time which points corretly to the .m2/repository.../joda-time/2.8.2 What this seems like is that when this runs from eclipse the path to the joda time jar file is (for lack of a better term) known. When the jar file is built however that path is not known. I opened the jar file and looked at the MANIFEST.MF file and I see:

Manifest-Version: 1.0
Built-By: John
Class-Path: joda-time-2.8.jar
Build-Jdk: 1.8.0_92
Created-By: Maven Integration for Eclipse
Main-Class: hello.HelloMain

The source is very simple: package hello;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;

public class HelloMain {

 public static void main(String[] args) {
    System.out.println("Hello From My Main ! It worked\n");
    final DateTime today = new DateTime().withZone(DateTimeZone.UTC);
    DateTime tommorrow = today.plusDays(3);

    String startTime = today.toString(DateTimeFormat.forPattern("yyyy-MM- dd'T'HH:mm'Z"));
    String endTime = tommorrow.toString(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm'Z"));
    System.out.printf("The start time %s  End Time %s \n", startTime, endTime);

    }

}

This is my pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hello</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>hello.HelloMain</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
    </build>
 <dependencies>
 <!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
  <dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.8</version> 
  </dependency>

TKPhillyBurb
  • 91
  • 3
  • 11
  • It sounds like what you really want is for maven to create a [jar with dependencies](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – nasukkin Jul 06 '16 at 19:03
  • could be that you are missing a slash or is a typo? C:\Users\username.m2\repository, maybe is C:\Users\username\.m2\repository – OscarBcn Jul 06 '16 at 19:04
  • Try `mvn compile` from the command line instead of using eclipse's tool, and see if it compiles that way. – Eli Sadoff Jul 06 '16 at 19:10
  • @Eli Sadoff I did the mvn compile and it does compile. – TKPhillyBurb Jul 06 '16 at 20:12

1 Answers1

0

After trying many different approaches to the Pom file I had settled on a different approach. This would only apply if your using Eclipse to do this as Eclipse offers an Export to runnable jar feature. While in Eclipse highlight your project of interest, right click to Export and select Java/Runnable JAR file. Click next and select "Package required libraries into generated JAR". This built the jar file with all of the required jar files I needed. While this really does not solve the problem from the perspective of creating the output jar using the maven packaging it did provide a proper executable jar file.

TKPhillyBurb
  • 91
  • 3
  • 11