在 Java Web 项目中编译 Servlet 和其他类 [英] Compile Servlets and others classes in a Java Web project

查看:29
本文介绍了在 Java Web 项目中编译 Servlet 和其他类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手,我正在尝试仅使用命令行在 linux 上编译我的 Servlet.在浏览器上看到此错误后,我决定这样做:命令 javac -classpath/opt/tomcat/lib/servlet-api.jar ComputerSV.java 给出以下错误:

I'm new in Java and I'm trying to compile my Servlet on linux using only the command-line. I decided do that after see this error on my browser: The command javac -classpath /opt/tomcat/lib/servlet-api.jar ComputerSV.java gives the following error:

ComputerSV.java:13: error: cannot find symbol
        ArrayList<Computer> computers = new ArrayList<>();
                  ^
  symbol:   class Computer
  location: class ComputerSV
ComputerSV.java:15: error: cannot find symbol
            new Computer(
                ^
  symbol:   class Computer
  location: class ComputerSV
ComputerSV.java:25: error: cannot find symbol
            new Computer(
                ^
  symbol:   class Computer
  location: class ComputerSV
3 errors

而我的 Servlet 源代码是:

and my Servlet source code is:

package com.lcdss.compmng.controller;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;[Imgur](http://i.imgur.com/D6rN3UA.png)
import javax.servlet.http.HttpServletResponse;

class ComputerSV extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        ArrayList<Computer> computers = new ArrayList<>();
        computers.add(
            new Computer(
                1,
                "HP",
                "hostname",
                "Windows 10 Pro x64",
                "Intel I7 7700K 4.2 GHz",
                2048,
                8196,
                "Anapolis"
            ),
            new Computer(
                2,
                "DELL",
                "hostname",
                "Windows 10 Home Basic x64",
                "Intel I7 7500U 2.5 GHz",
                512,
                4098,
                "Goiania"
            )
        );

        request.setAttribute("computers", computers);
        request.getRequestDispatcher("computer/index.jsp").forward(request, response);
    }
}

我使用 tomcat 作为 Web 服务器和文本编辑器 (Atom) 来帮助我完成这个挑战(以及现在的 stackoverflow).我现在的问题是编译器没有找到我已经编译的类 Computer 但没有成功修复这个错误.

I'm using tomcat as a web server and just a text editor (Atom) to help me complete this challenge (and the stackoverflow now). I now the problem is that the compiler isn't finding my class Computer that I already compiled but no success to fix this error.

推荐答案

我错过了对 Computer 类的导入,并且在上面的示例中出现了语法错误.

I have missed the import to the Computer class and had a syntax error in the example above.

package com.lcdss.compmng.controller;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lcdss.compmng.entity.Computer;

public class ComputerSV extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        ArrayList<Computer> computers = new ArrayList<>();

        computers.add(
            new Computer(
                1,
                "HP",
                "hostname",
                "Windows 10 Pro x64",
                "Intel I7 7700K 4.2 GHz",
                2048,
                8196,
                "Anapolis"
            )
        );

        computers.add(
            new Computer(
                2,
                "DELL",
                "hostname",
                "Windows 10 Home Basic x64",
                "Intel I7 7500U 2.5 GHz",
                512,
                4098,
                "Goiania"
            )
        );

        request.setAttribute("computers", computers);
        request.getRequestDispatcher("computer/index.jsp").forward(request, response);
    }
}

为了编译,我使用了命令 javac -cp/opt/tomcat/webapps/compmng/WEB-INF/classes:/opt/tomcat/lib/servlet-api.jar ComputerSV.java.第一个类路径 (cp) 通知编译器我的类在哪里以及包名是什么.

To compile I used the command javac -cp /opt/tomcat/webapps/compmng/WEB-INF/classes:/opt/tomcat/lib/servlet-api.jar ComputerSV.java. The first classpath (cp) informs to the compiler where my classes are and what is the package name.

这篇关于在 Java Web 项目中编译 Servlet 和其他类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆