![](/img/trans.png)
[英]Why are my Java code changes not being reflected when ran from command line but working in Eclipse?
[英]My java web server works in eclipse but it crashes every time if ran through the command line
我有一个非常简单的Web服务器,它可以接受客户端连接,检索静态网页和服务Servlet。
我用Java编写了Web服务器,并且正在使用Tomcat的Tomcat库。 我通过在Eclipse IDE中运行它来测试了所有代码,并且一切正常,但是当我通过命令行运行代码时,它将正确启动主程序,但是当我尝试通过浏览器访问网页时( http: //localhost/hello.html ),则应用程序崩溃,这是示例输出:
p1>java p1Solution.WebServer 80
Waiting for a connection.
Servicing the connection.
Closing the connection and shutting down the executor.
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/Servlet
Exception
at p1Solution.WebServer.main(WebServer.java:54)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 1 more
这是我的Web服务器
public class WebServer {
private static final ExecutorService executor = Executors.newCachedThreadPool();
public static void main(String[] args) throws Exception {
int port = (args.length > 0) ? Integer.parseInt(args[0]) : 80;
// listen for clients on the port specified as a command line argument
ServerSocket serverSocket = new ServerSocket(port);
Socket connection = null;
try {
while (true) {
try {
System.out.println("Waiting for a connection.");
// Wait for an in bound client connection
connection = serverSocket.accept();
System.out.println("Servicing the connection.");
// Handle the client connection in a separate thread
executor.execute(new ConnectionHandler(connection));
} catch (Exception e) {
// Never crash the server
e.printStackTrace();
}
}
}finally {
System.out.println("Closing the connection and shutting down the executor.");
connection = null;
serverSocket.close();
executor.shutdown();
executor.awaitTermination(3, TimeUnit.SECONDS);
}
}
}
我一直在搜寻我的例外情况,有些人一直在谈论有关类路径的内容,但是我还没有找到确切的答案。 当我在Eclipse中运行程序时,一切正常!
这是我的项目的目录(我的项目所在的目录):
p1>ls
.classpath bin hello.html p1Solution.jar
.project coreservlets p1Solution testsuite
这是我的p1Solution(代码所在的位置)的目录内容:
p1>ls p1Solution
ConnectionHandler.class MyHttpServletRequest.class
ConnectionHandler.java MyHttpServletRequest.java
HttpServletRequestAdapter.class MyHttpServletResponse.class
HttpServletRequestAdapter.java MyHttpServletResponse.java
HttpServletResponseAdapter.class WebServer.class
HttpServletResponseAdapter.java WebServer.java
有人可以帮我解决这个问题吗? 有人看过这个问题吗?
PS:是的,我在Windows中确实有'ls'命令,这不是魔术! 如果您不相信我,那么Google会“为Windows着迷”。
您对javax.servlet.Servlet
具有满意的编译时依赖关系,但是在运行时不存在,因此NoClassDefFoundError
。 因此,您需要将此类的实现放在类路径上(由Tomcat的servlet-api.jar
)。 为此,建议的方法是使用-cp
选项:
p1>java -cp c:\path\to\servlet-api.jar;. p1Solution.WebServer 80
不要忘记-cp C:\\path\\to\\servlet-api.jar;.
的“ .
”(当前目录)。 还要注意,我不确定路径名中的空格如何处理(我不在Windows下,无法测试)。 因此,要么使用Windows短名称8.3(例如C:\\PROGRA~1\\
),要么不要在路径名中使用空格或将此库移到其他位置。
使用“导出-> Runnable Jar”工具来创建一个jar文件,该jar文件包含在下面并被正确引用的依赖库(因为在运行时类路径中没有servlet.jar)。 那你就可以跑
java -jar p1solution.jar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.