Ich brauche einen leichten Server nur für Testzwecke, nach einigen googeln ich this Antwort gefunden, obwohl meine Datei ohne Problem kompiliert, aber beim Laufen Ich erhalte die folgende Spur zurück:Ant Problem beim Laufen nativen HTTP-Servers
[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
Quelldatei
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 Buildregel
<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>
Update:
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)
aktualisieren 2
Es gab einige Problem im Glas nach JAR-Datei aktualisieren Ich bin nicht folgende Fehler bekommen:
java.lang.VerifyError: (class: com/sun/net/httpserver/spi/HttpServerProvider$1, method: run signature:()Ljava/lang/Object;) Incompatible argument to function
Welche Version von Java sind Sie? – rmlan
@rmlan aktualisierte Frage mit Java-Version. – CodeYogi
dieses Ziel hinzufügen und starten Sie es Java/JVM-Version: $ {} ant.java.version Java/JVM Detail Version: $ {} java.version –