我正在Eclipse中的Maven项目中实现描述为here的Datastax Java驱动程序。 我按照描述在pom.xml中添加了三个依赖项:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-extras</artifactId>
<version>3.1.4</version>
</dependency>
然后我实现了this manual中描述的类。
package CasConnector.CasConnector;
import com.datastax.driver.core.*;
public class CasConnector {
public static void main(String[] args) {
Cluster cluster = null;
try {
cluster = Cluster.builder() // (1)
.addContactPoint("127.0.0.1")
.build();
Session session = cluster.connect(); // (2)
ResultSet rs = session.execute("select release_version from system.local"); // (3)
Row row = rs.one();
System.out.println(row.getString("release_version")); // (4)
} finally {
if (cluster != null) cluster.close(); // (5)
}
}
}
我将以下JAR文件添加到&#34;参考库&#34;目录(添加到构建路径):
然后我将项目导出为JAR文件以在Linux上执行,但我收到了以下错误。似乎无法加载Cluster类,但我在包含它的Maven项目中添加了cassandra-driver-core-3.1.4依赖项。请在我的配置中建议可能出错或丢失的内容。
# java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/datastax/driver/core/Cluster
at CasConnector.CasConnector.CasConnector.main(CasConnector.java:14)
Caused by: java.lang.ClassNotFoundException: com.datastax.driver.core.Cluster
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
S O L U T I O N:
1。将以下插件添加到pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Path.to.Main.Class</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
2。右键单击您的项目 - &gt; &#39; Run As&#39; - &GT; &#39; Maven Build&#39; - &GT;键入&#34; clean install&#34; - &GT; &#39;应用&#39; - &GT; &#39;运行&#39;
生成的JAR文件将在&#34; target&#34;下创建。包含所有必需依赖项的目录。
答案 0 :(得分:0)
我认为您错过了test.jar
的第三方依赖关系。如果您使用Maven
来构建项目,请尝试使用maven plugin
创建JAR,而不是将Eclipse 导出项目用作jar 功能。要create an executable JAR with dependencies using Maven,您可以使用maven-shade-plugin
或maven-assembly-plugin
maven插件。