简体   繁体   English

在Java Web项目中编译Servlet和其他类

[英]Compile Servlets and others classes in a Java Web project

I'm new in Java and I'm trying to compile my Servlet on linux using only the command-line. 我是Java的新手,正在尝试仅使用命令行在Linux上编译Servlet。 I decided do that after see this error on my browser: 在浏览器中看到此错误后,我决定这样做: https://i.imgur.com/D6rN3UA.png The command javac -classpath /opt/tomcat/lib/servlet-api.jar ComputerSV.java gives the following error: 命令javac -classpath /opt/tomcat/lib/servlet-api.jar ComputerSV.java给出以下错误:

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

and my Servlet source code is: 我的Servlet源代码是:

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);
    }
}

I'm using tomcat as a web server and just a text editor (Atom) to help me complete this challenge (and the stackoverflow now). 我将tomcat用作Web服务器,而只是使用文本编辑器(Atom)来帮助我完成这一挑战(以及现在的stackoverflow)。 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. 我现在的问题是,编译器找不到我已经编译的计算机类,但没有成功解决此错误。

Its not related to tomcat.It is a normal compilation problem. 它与tomcat无关,这是一个正常的编译问题。 During the compilation time/phase, when ArrayList<Computer> computers = new ArrayList<>(); 在编译时间/阶段,当ArrayList<Computer> computers = new ArrayList<>(); is being compiled,jvm will look for class Computer because you have specified that your arraylist will contain only this type.But Since there is no Computer class at this point of time,it will thow an exception, ClassNotFoundException .To successfully run this,better first create a class Computer ,compile it and then compile this class. 正在被编译时,jvm将查找类Computer,因为您已指定arraylist仅包含此类型。但是由于此时没有Computer类,它将引发ClassNotFoundException异常。要成功运行该类,更好首先创建一个Computer类,对其进行编译,然后再编译该类。

I have missed the import to the Computer class and had a syntax error in the example above. 我错过了导入到Computer类的过程,并且在上面的示例中出现语法错误。

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);
    }
}

To compile I used the command javac -cp /opt/tomcat/webapps/compmng/WEB-INF/classes:/opt/tomcat/lib/servlet-api.jar ComputerSV.java . 要进行编译,我使用了命令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. 第一个类路径(cp)通知编译器我的类在哪里以及包名称是什么。

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

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