问题描述
我的文件夹布局是:
/web-inf/web.xml
/web-inf/classes/
HelloWorld.java:
HelloWorld.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");
}
}
我想做以下事情:
- 通过命令行构建所有内容
- 使用.war文件发布到Tomcat的webapps目录
我应该在哪里放置 HelloWorld.java
文件,以及如何使用命令行编译它?
我相信 HelloWorld.java
的输出位于 / classes /
文件夹中。
Where should I put my HelloWorld.java
file, and how to compile it using the command line?I believe the output of the HelloWorld.java
goes in the /classes/
folder.
然后如何生成 .war
文件?
我尝试像这样手动编译:
I tried just manually compiling like this:
javac HelloWorld.java
我得到了与进口相关的错误,即它无法在我的路径中找到库。是否有我需要下载的Java EE库并将其添加到我的路径中?
And I got errors relating to the imports i.e. it couldn't find the libraries in my path. Is there a Java EE library I have to download and add it to my path?
推荐答案
您的代码依赖于servlet API不在核心Java库中。您需要将它添加到类路径中:
Your code depends on the servlet API which is not in the core Java library. You will need to add it to your classpath:
javac -classpath $TOMCAT_HOME/lib/servlet-api.jar yourpackage/HelloWorld.java
WAR与JAR(ZIP与UTF-8文件名)共享相同的文件格式。一般来说,您可以使用ZIP实用程序来处理它们。
WARs share the same file format as JARs (ZIP with UTF-8 filenames.) Generally, you can use ZIP utilities to work with them.
WAR结构可能如下所示:
A WAR structure might look like this:
index.html
foo.jsp
WEB-INF/classes/yourpackage/HelloWorld.class
WEB-INF/web.xml
上面假设你的.packa文件中有包yourpackage;
声明。应避免使用默认包。
The above assumes you have a package yourpackage;
declaration in your .java file. The default package should be avoided.
这篇关于尝试从命令行构建,并生成WAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!