Compile Servlets and others classes in a Java Web project

牧云@^-^@ 提交于 2019-12-12 02:16:33

问题


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

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

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.


回答1:


Its not related to tomcat.It is a normal compilation problem. During the compilation time/phase, when 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.




回答2:


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

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.



来源:https://stackoverflow.com/questions/42802798/compile-servlets-and-others-classes-in-a-java-web-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!