Reputation: 175
I am really struggling trying to get my maven project to run in the command line. It runs how I want it to in eclipse, but using mvn package command in the terminal runs the unit tests that I wrote, but not the actual main source code application that I want it to run.
java -cp target/TestKata-0.0.1-SNAPSHOT.jar com.techelevator.Main
Returns this error:
Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/OkHttpClient
at com.techelevator.Main.<clinit>(Main.java:12)
Caused by: java.lang.ClassNotFoundException: okhttp3.OkHttpClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I assume I'm getting this error, because the dependencies are not included in the SNAPSHOT.jar file and it fails after it gets to the first line where a dependency is required?? Any help is greatly appreciated, I am very new to this stuff so I apologize if I'm doing something really stupid.
EDIT: Here is my pom.xml
<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>com.techelevator</groupId>
<artifactId>TestKata</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium</artifactId>
<version>2.0rc2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehouse.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.techelevator.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Upvotes: 2
Views: 855
Reputation: 30696
you must package your dependencies into your jar via maven-assemble-plugin.the jar-with-dependnecies
descriptor find in maven
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
you can add executions when you run mvn package
command,that will output a jar filename like *-with-jar-dependencies.jar
and then run that jar file instead via java
,e.g:java -cp target/TestKata-0.0.1-SNAPSHOT-with-jar-dependencies.jar com.techelevator.Main
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Upvotes: 2