java 使用 javac 编译 servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5791446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:46:23  来源:igfitidea点击:

Compiling servlets with javac

javaservletsjavac

提问by Pieter

I want to compile servlets outside of NetBeans. I made a simple Hello World servlet that produced these compiler errors.

我想在 NetBeans 之外编译 servlet。我制作了一个简单的 Hello World servlet,它产生了这些编译器错误。

import javax.servlet.ServletException;                                                           
                    ^                                                                            
ServletTester.java:4: package javax.servlet.http does not exist                                  
import javax.servlet.http.*;                                                                     
^                                                                                                
ServletTester.java:6: cannot find symbol                                                         
symbol: class HttpServlet                                                                        
public class ServletTester extends HttpServlet {                                                 
                                   ^                                                             
ServletTester.java:7: cannot find symbol                                                         
symbol  : class HttpServletRequest                                                               
location: class ServletTester                                                                    
        protected void processRequest(HttpServletRequest request, HttpServletResponse response) t
                                      ^
ServletTester.java:7: cannot find symbol
symbol  : class HttpServletResponse
location: class ServletTester
        protected void processRequest(HttpServletRequest request, HttpServletResponse response) t
                                                                  ^
ServletTester.java:7: cannot find symbol
symbol  : class ServletException
location: class ServletTester
        protected void processRequest(HttpServletRequest request, HttpServletResponse response) t

6 errors

Clearly, the javax.servletpackage cannot be located. I have javax.servlet.jarfrom a GlassFish install, but if I do javac ServletTester.java -classpath /opt/glassfish3/glassfish/modules/I still get the same errors.

显然,javax.servlet无法找到包裹。我javax.servlet.jar从 GlassFish 安装,但如果我这样做,javac ServletTester.java -classpath /opt/glassfish3/glassfish/modules/我仍然会遇到相同的错误。

What is the proper way to manually compile servlets?

手动编译 servlet 的正确方法是什么?

回答by Pablo Santa Cruz

Try this:

试试这个:

$ javac -classpath .:/opt/glassfish3/glassfish/modules/javax.servlet.jar ServletTester.java 

Please note that JAR filenames in classpathmust be fully specified. Just their containing directory is not enough. Using wildcards is also allowed (as appointed by one comment).

请注意,必须完全指定类路径中的 JAR 文件名。仅仅包含它们的目录是不够的。也允许使用通配符(由一条评论指定)。

回答by Joseph Ottinger

Plus, consider maven. It has archetypes that give you working boilerplate for generating web applications with many, many different application frameworks; there's a simple web app archetype that would automate what you're trying to do here.

另外,考虑maven。它具有原型,可为您提供用于生成具有许多不同应用程序框架的 Web 应用程序的工作样板;有一个简单的 Web 应用程序原型可以自动执行您在此处尝试执行的操作。

Manually compiling javausually isn't a good plan; build tools exist to automate the processing and lifecycle of projects. They're mature and useful.

手动编译java通常不是一个好的计划;存在构建工具来自动化项目的处理和生命周期。他们成熟而有用。