Deslyxia
Deslyxia

Reputation: 629

Can run a Maven project within IntelliJ but not from Terminal

I have created a console application in IntelliJ that utilizes Maven. From within IntelliJ I can compile and run the app with no issues ...

From terminal however i execute the following commands (in the same dir with pom.xml)

mvn Install -U
java -classpath target/myApp-2.0-SNAPSHOT.jar MainClass

The install command seems to build the jar file without any issue. The second command gives me the following error

Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonParseException

In my pom.xml my dependencies are as follows

<dependencies>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.5.0</version>
    </dependency>
</dependencies>

It seems to me that its not pulling in the Jackson Dependencies but im not sure what im missing here.

Upvotes: 0

Views: 2613

Answers (2)

Francois Laroche
Francois Laroche

Reputation: 819

As said, the problem you have is that your jar needs the other jars to execute.

I see 3 solutions :

1- As stated above, when you run the program, add the -classpath argument

2- Use the maven-jar-plugin to add the dependencies in the manifest, then you'll only have to have the dependencies at the requested place to have all execute. See http://maven.apache.org/shared/maven-archiver/examples/classpath.html#aAdd

3- Package the dependencies inside your jar with a plugin like jarjar : http://sonatype.github.io/jarjar-maven-plugin/

This will create you a standalone jar

Hope it can help.

Upvotes: 1

Mark Peters
Mark Peters

Reputation: 81074

Maven has no impact on your runtime classpath (only your compile-time classpath). You need to add your dependencies to the classpath.

Upvotes: 1

Related Questions