简体   繁体   中英

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.

I want this sensordata to be emitted to Apache Kafka through a producer, setted up in the servlet. 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.

But on compiling with: 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.

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.

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.

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

The import for the 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. I recommend using the ProducerRecord.class (worked for my test-example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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