我试图从Kafka site编译一些示例代码。当scala尝试读取java属性时,它会给出运行时错误。
错误:
Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Ljava/lang/String;
at kafka.utils.VerifiableProperties.getShortInRange(VerifiableProperties.scala:83)
at kafka.utils.VerifiableProperties.getShort(VerifiableProperties.scala:59)
at kafka.producer.SyncProducerConfigShared$class.$init$(SyncProducerConfig.scala:49)
at kafka.producer.ProducerConfig.<init>(ProducerConfig.scala:51)
at kafka.producer.ProducerConfig.<init>(ProducerConfig.scala:56)
at Test_Producer.main(Test_Producer.java:28)
以下是有问题的代码片段。错误来自new ProducerConfig(props)
语句(上面第28行)。
Properties props = new Properties();
props.put("metadata.broker.list", "broker1:9092,broker2:9092");
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("partitioner.class", "example.producer.SimplePartitioner");
props.put("request.required.acks", "1");
ProducerConfig config = new ProducerConfig(props);
我试着踩到有问题的代码,似乎所有相关的值都已设置(IE getProperties正在查找值)。
代码构建正常,但也许我使用了错误的库?
建议?
编辑:
代码在Intellij 14中运行(或应该运行)。
Java是1.8 Scala库是2.11.6
另一个数据点:如果我注释掉除第一个属性之外的所有属性(&#39; broker1:9092 ......&#39;)我会收到此错误:
Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Ljava/lang/String;
at kafka.producer.ProducerConfig.liftedTree1$1(ProducerConfig.scala:83)
at kafka.producer.ProducerConfig.<init>(ProducerConfig.scala:82)
at kafka.producer.ProducerConfig.<init>(ProducerConfig.scala:56)
at Test_Producer.main(Test_Producer.java:28)
相似 - 但不完全相同。仍然让我觉得它是一个图书馆问题。
答案 0 :(得分:1)
使用以下kafka POM依赖项遇到此问题:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<version>0.8.2.0</version>
<scope>compile</scope>
</dependency>
错误堆栈:
Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Lscala/collection/immutable/StringOps;
at kafka.utils.VerifiableProperties.getShortInRange(VerifiableProperties.scala:85)
at kafka.producer.SyncProducerConfigShared$class.$init$(SyncProducerConfig.scala:53)
at kafka.producer.ProducerConfig.<init>(ProducerConfig.scala:51)
at kafka.producer.ProducerConfig.<init>(ProducerConfig.scala:56)
at dominus.intg.jms.kafka.KafkaProducer.main(KafkaProducer.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
升级到0.8.2.1,此问题已不复存在。
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.8.2.1</version>
<scope>compile</scope>
</dependency>