![](/img/trans.png)
[英]Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
[英]Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pulsar/client/api/PulsarClient
我試圖通過java執行apache pulsar生產者和消費者程序,我在GCP虛擬機中安裝了apache-pulsar standalone並以獨立模式啟動了集群。 下一步是我在 windows eclipse 中提供了一個 maven 構建,並在 GCP 機器中上傳了相同的 jar 文件當我在 windows eclipse 中執行生產者和消費者程序時,我收到連接被拒絕的錯誤,這很明顯,因為 pulsar 沒有安裝在 windows 機器中。 但是,當我在 GCP 實例中嘗試相同的事情時,即使集群已經啟動,我也會收到與 no class def found 錯誤相關的錯誤。
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>pulsar-client-project</groupId>
<artifactId>pulsar-client-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>pulsar-client-project</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client</artifactId>
<version>2.0.1-incubating</version>
</dependency>
</dependencies>
</project>
ProducerTutorial.java >>>
package pulsar_client_project.pulsar_client_project;
import org.apache.pulsar.client.api.CompressionType;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageBuilder;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.stream.IntStream;
public class ProducerTutorial {
// private static final Logger log =
LoggerFactory.getLogger(ProducerTutorial.class);
private static final String SERVICE_URL = "pulsar://localhost:6650";
private static final String TOPIC_NAME = "my-topic";
public static void main(String[] args) throws IOException {
System.out.println("inside main");
// Create a Pulsar client instance. A single instance can be shared across many
// producers and consumer within the same application
PulsarClient client = PulsarClient.builder()
.serviceUrl(SERVICE_URL)
.build();
System.out.println("client.."+client);
// Here you get the chance to configure producer specific settings
Producer<byte[]> producer = client.newProducer()
// Set the topic
.topic(TOPIC_NAME)
// Enable compression
.compressionType(CompressionType.LZ4)
.create();
System.out.println("producer.."+producer);
// Once the producer is created, it can be used for the entire application life-cycle
// log.info("Created producer for the topic {}", TOPIC_NAME);
// Send 10 test messages
IntStream.range(1, 11).forEach(i -> {
String content = String.format("hello-pulsar-%d", i);
// Build a message object
Message<byte[]> msg = MessageBuilder.create()
.setContent(content.getBytes())
.build();
// Send each message and log message content and ID when successfully received
try {
MessageId msgId = producer.send(msg);
//log.info("Published message '{}' with the ID {}", content, msgId);
} catch (PulsarClientException e) {
//log.error(e.getMessage());
}
});
client.close();
}
}
消費者教程 >>>>
package pulsar_client_project.pulsar_client_project;
import java.io.IOException;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.SubscriptionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConsumerTutorial {
//private static final Logger log =
LoggerFactory.getLogger(ConsumerTutorial.class);
private static final String SERVICE_URL = "pulsar://localhost:6650";
private static final String TOPIC_NAME = "my-topic";
private static final String SUBSCRIPTION_NAME = "my-subscription";
public static void main(String[] args) throws IOException {
// Create a Pulsar client instance. A single instance can be shared
across many
// producers and consumer within the same application
System.out.println("inside main");
PulsarClient client = PulsarClient.builder()
.serviceUrl(SERVICE_URL)
.build();
System.out.println("client.."+client);
// Here you get the chance to configure consumer specific settings. eg:
Consumer<byte[]> consumer = client.newConsumer()
.topic(TOPIC_NAME)
// Allow multiple consumers to attach to the same subscription
// and get messages dispatched as a queue
.subscriptionType(SubscriptionType.Shared)
.subscriptionName(SUBSCRIPTION_NAME)
.subscribe();
System.out.println("consumer.."+consumer);
// Once the consumer is created, it can be used for the entire application lifecycle
//log.info("Created consumer for the topic {}", TOPIC_NAME);
do {
// Wait until a message is available
Message<byte[]> msg = consumer.receive();
// Extract the message as a printable string and then log
String content = new String(msg.getData());
// log.info("Received message '{}' with ID {}", content, msg.getMessageId());
// Acknowledge processing of the message so that it can be deleted
consumer.acknowledge(msg);
} while (true);
}
}
那么,ubuntu/GCP VM 機器的合適解決方案是什么。 我做錯了什么,請給我方向
提前致謝
ClassNotFoundException表示在您的classpath 中找不到該類。
當您從 Eclipse 本地運行時,當您按下運行按鈕時,脈沖星客戶端類對 Eclipse 正在調用的 Java 進程可見。 但是,當您在 GCP 機器上運行 jar 時,您的類路徑中沒有包含來自 pulsar-client jar 的類,這就是它們丟失的原因:
java -cp pulsar-client-project-0.0.1-SNAPSHOT.jar pulsar_client_project.pulsar_client_project.ConsumerTutorial
上面的命令指出java進程的類路徑是標准的JDK類和來自jar pulsar-client-project-0.0.1-SNAPSHOT
的類,要運行的類(main-class)是pulsar_client_project.pulsar_client_project.ConsumerTutorial
。
您需要做的是在 GCP 機器上提供 pulsar-client jar 的副本,並將其包含在您的類路徑中。 例如
java -cp pulsar-client-project-0.0.1-SNAPSHOT.jar:pulsar-client.jar pulsar_client_project.pulsar_client_project.ConsumerTutorial
或者
java -cp pulsar-client-project-0.0.1-SNAPSHOT.jar;pulsar-client.jar pulsar_client_project.pulsar_client_project.ConsumerTutorial
取決於 GCP 機器操作系統( :
通常是類似 Unix 的操作系統上的路徑分隔符,而;
通常是 Windows 系統上的路徑分隔符)。 在本地運行時,Eclipse 會自動完成類似的操作。
可能更簡單的方法是在生成的 jar 文件中包含所有項目依賴項。 如果你像這樣修改你的pom:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pulsar-client-project</groupId>
<artifactId>pulsar-client-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>pulsar-client-project</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client</artifactId>
<version>2.0.1-incubating</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
這里定義了程序集插件,它將包含生成的 jar 文件target/pulsar-client-project-0.0.1-SNAPSHOT-jar-with-dependencies
。 您可以看到它的大小約為 25 MB,而原始target/pulsar-client-project-0.0.1-SNAPSHOT.jar
約為 5KB。 您還可以使用任何支持 zip 的存檔程序(winzip、peazip、unzip...)打開jar並檢查其內容,因為jar 文件格式是基於 zip 文件格式構建的。
現在,當您將較大的 jar 文件復制到 GCP 機器時,您應該能夠像這樣運行它:
java -cp pulsar-client-project-0.0.1-SNAPSHOT-jar-with-dependencies.jar pulsar_client_project.pulsar_client_project.ConsumerTutorial
或者
java -cp pulsar-client-project-0.0.1-SNAPSHOT-jar-with-dependencies.jar pulsar_client_project.pulsar_client_project.ProducerTutorial
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.