我需要一个轻量级的服务器只是为了测试的目的,经过一些谷歌搜索我找到了这个答案,虽然我的文件编译没有任何问题,但在运行时,我得到以下跟踪:
[java] java.lang.NoClassDefFoundError: com/sun/net/httpserver/HttpHandler
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:771)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:221)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:135)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:497)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.Target.execute(Target.java:435)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:456)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
[java] at org.apache.tools.ant.Main.runBuild(Main.java:851)
[java] at org.apache.tools.ant.Main.startAnt(Main.java:235)
[java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
[java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
[java] Caused by: java.lang.NoClassDefFoundError: com/sun/net/httpserver/HttpHandler
[java] at java.lang.Class.forName0(Native Method)
[java] at java.lang.Class.forName(Class.java:344)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:135)
[java] ... 21 more
[java] Caused by: java.lang.ClassNotFoundException: com.sun.net.httpserver.HttpHandler
[java] at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1374)
[java] at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1323)
[java] at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1082)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] ... 24 more
[java] Java Result: -1
源文件package web.src.example;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class Server {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/test", new QuoteHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class QuoteHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
String response = "Bazingaaaa";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
Ant构建规则
<path id="classpath.server">
<pathelement location="lib/http-20070405.jar" />
</path>
<target name="server">
<mkdir dir="${delivery.build.dir}"/>
<javac srcdir="${delivery.src.dir}" destdir="${delivery.build.dir}" includeantruntime="false">
<classpath refid="classpath.server"/>
</javac>
</target>
<target name="runserver">
<java classname="web.src.example.Server">
<classpath>
<path refid="classpath.server" />
<pathelement location="build/delivery"/>
</classpath>
</java>
</target>
更新:
openjdk version "1.8.0_40-internal"
OpenJDK Runtime Environment (build 1.8.0_40-internal-b09)
OpenJDK 64-Bit Server VM (build 25.40-b13, mixed mode)
更新2
在更新jar文件后,jar中有一些问题,我没有得到以下错误:
java.lang.VerifyError: (class: com/sun/net/httpserver/spi/HttpServerProvider$1, method: run signature: ()Ljava/lang/Object;) Incompatible argument to function
您似乎正在使用源代码jar,而不是包含编译后的类的jar:
<path id="classpath.server">
<pathelement location="lib/http-20070405-sources.jar" />
</path>
我建议从Maven中心下载正确的jar:
- http://search.maven.org/remotecontent?filepath=com/sun/net/httpserver/http/20070405/http - 20070405. - jar