Reputation: 7664
I created a new project to set Google Guava.
This is my POM.xml.
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>InetAddressTest</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
This is my main method.
public static void main(String[] args) throws Exception {
try {
System.out.println(InetAddresses.isInetAddress("127.0.0.1"));
} catch (NoClassDefFoundError exp) {
System.out.println(exp);
}
}
I am able to run it inside my IDE.
I can package it with mvn package
When I run it java -jar target/<NAME>.jar
, it throws an exception java.lang.NoClassDefFoundError: com/google/common/net/InetAddresses
I tried to browse the solutions. But they haven't worked so far.
I am guessing (based on other problems) that I am missing some dependencies for Guava
?
Upvotes: 0
Views: 405
Reputation: 3310
Looks like google guava
itself is not in the class path, try setting the class path as below to your java
command:
java -jar -classpath .:./{path_to_gauva_jar} target/<name>.Jar
Change the separator as per your operating system.
Upvotes: 1
Reputation: 1025
You have two options :-
1) While executing the Java command use the -cp or -classpath option to list all the required jar files.
2) Include all the dependencies in your jar by using the maven shade plugin in your pom.xml
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<finalName>uber-${project.artifactId}-${project.version}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Upvotes: 1