简体   繁体   English

如何将程序包从jar文件导入到在Tomcat 8上运行的servlet中?

[英]How do I import packages from a jar-file, into a servlet, which is running on tomcat 8?

I'm currently working on setting up an tomcat8-server and want to write an servlet on it, which handles POST-requests with sensorvalues attached to it. 我目前正在设置一个tomcat8服务器,并希望在其上编写一个servlet,该servlet可以处理附加有sensorvalue的POST请求。

I want this sensordata to be emitted to Apache Kafka through a producer, setted up in the servlet. 我希望此传感器数据通过在servlet中设置的生产者发送到Apache Kafka。 I wanted to set up a test-http-servlet with the code from " How to send a message via Kafka.Producer from a java servlet to Kafka " to learn writing my own. 我想用“ 如何通过Kafka.Producer从Java servlet到Kafka发送消息 ”中的代码建立一个test-http-servlet,以学习编写自己的代码。

But on compiling with: javac -cp /home/tobias/apache-tomcat-8.5.8/lib/servlet-api.jar HTTPServlet.java 但是在使用以下代码进行编译时: javac -cp /home/tobias/apache-tomcat-8.5.8/lib/servlet-api.jar HTTPServlet.java

, I get the following message: ,我收到以下消息:

HTTPServlet.java:19: error: package kafka.producer does not exist import kafka.producer.ProducerConfig; ^ HTTPServlet.java:20: error: package kafka.javaapi.producer does not exist import kafka.javaapi.producer.ProducerData; ^

I did already put the "kafka_2.11-0.10.1.0.jar", which contains the "kafka.producer.ProducerConfig"-class into the "WEB-INF/lib" folder, but It does not seem to work. 我确实已经将包含“ kafka.producer.ProducerConfig”类的“ kafka_2.11-0.10.1.0.jar”放到“ WEB-INF / lib”文件夹中,但是它似乎不起作用。

This "kafka_2.11-0.10.1.0.jar" was located in the "libs" folder of "kafka_2.11-0.10.1.0", which can be downloaded from the kafka-website. 该“ kafka_2.11-0.10.1.0.jar”位于“ kafka_2.11-0.10.1.0”的“ libs”文件夹中,可以从kafka网站下载该文件夹。

So I wanted to ask, how I would get this working and If anyone knows, where the 2. missing file is located in the latest release of kafka. 因此,我想问一下,我将如何使它正常工作?如果有人知道,最新的kafka版本中2.丢失的文件位于何处。

I solved the problem ( thanks to Strelok ) by adding the additional api.jar-files to my compile-command: javac -cp /home/tobias/apache-tomcat-8.5.8/lib/servlet-api.jar:/home/tobias /apache-tomcat-8.5.8/webapps/HTTPServlet/WEB-INF/lib/kafka_2.11-0.10.1.0.jar HTTPServlet.java 我通过将其他api.jar文件添加到我的编译命令中来解决了这个问题( 感谢Strelok ): javac -cp /home/tobias/apache-tomcat-8.5.8/lib/servlet-api.jar:/home/tobias /apache-tomcat-8.5.8/webapps/HTTPServlet/WEB-INF/lib/kafka_2.11-0.10.1.0.jar HTTPServlet.java

The import for the ProducerData: import kafka.javaapi.producer.ProducerData; ProducerData的导入: import kafka.javaapi.producer.ProducerData; is not possible anymore, because the ProducerData.class was removed from the 0.6 to 0.7 release of kafka. 不再可能,因为ProducerData.class已从kafka的0.6到0.7版本中删除。 I recommend using the ProducerRecord.class (worked for my test-example). 我建议使用ProducerRecord.class(适用于我的测试示例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM