Java WebApp:ClassNotFoundException com.rabbitmq.client.ConnectionFactory

时间:2015-12-06 11:08:50

标签: java maven servlets web-applications rabbitmq

我有以下maven依赖:

    <dependency>
        <groupId>com.rabbitmq</groupId>
        <artifactId>amqp-client</artifactId>
        <version>3.5.6</version>
    </dependency>

我有以下servlet:

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeoutException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import model.DVD;

/**
 * Servlet implementation class LoginServlet
 */
public class IndexServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet()
    {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException
    {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        String QUEUE_NAME = "hello";

        try
        {

            /// some code

            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();

            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            channel.basicPublish("", QUEUE_NAME, null, obj.getBytes());
            System.out.println(" [x] Sent '" + dvd.getName() + "'");

            channel.close();
            connection.close();
        } catch (IOException | TimeoutException | NumberFormatException exp)
        {
            exp.printStackTrace();
        }

        response.sendRedirect("/A3_Producer/");
    }

}

我使用干净安装运行Maven构建,然后在tomcat服务器中启动Web应用程序。欢迎页面加载,但当我点击提交按钮并输入此servlet时,我在尝试制作ClassNotFoundException对象时得到ConnectionFactory

我也添加了jar来构建路径,但它没有解决问题。

我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:0)

为了使其工作,您需要将客户端jar放入tomcatinstallation/lib lib文件夹中。 ($CATALINA_HOME/lib