Jetty 8 web sockets

2019-02-15 08:35发布

问题:

I've downloaded the latest Jetty 8 Hightide, but cannot get WebSockets to work. I unzip the distribution, put my .war file in the webapps folder and start:

java.lang.NoClassDefFoundError: org/eclipse/jetty/websocket/WebSocketServlet

I've verified that the JETTY_HOME/lib folder contains the jetty-websocket.jar and it contains the WebSocketServlet class file.

I've had this running before with the milestone releases.

回答1:

For some reason, it appears that web sockets are no longer available by default, so the following code is needed in the context XML file:

<Set name="extraClasspath"><SystemProperty name="jetty.home" default="."/>/lib/jetty-websocket-<version>.jar</Set>


回答2:

I experienced the same using jetty-maven-plugin and version 8.0.3.v20111011. However the solution was simple. Just include the jetty-websocket as a compiled scope dependency (i.e. don't declared scope).

Relevant sections of pom.xml:

...
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jetty.version>8.0.3.v20111011</jetty.version>
</properties>
...

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${jetty.version}</version>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-websocket</artifactId>
        <version>${jetty.version}</version>
    </dependency>
    ...
</dependencies>


回答3:

copy %JETTY_ROOT%\lib\jetty-websocket-<version>.jar %SERVLET_ROOT%\WEB-INF\lib

Seemed to work great for me!



回答4:

Had similar problem running Jetty from Eclipse via WST plugin. Here is how I solved it. Under workspace's .metadata directory found application's context: [workspace's .metadata]/.plugins/org.eclipse.wst.server.core/[configuration id like 'tmp1']/contexts/[app name].xml . Edited it to include the following under Configure tag: <Set name="extraClasspath">[jetty install dir]/lib/jetty-websocket-8.1.2.v20120308.jar;[jetty install dir]/lib/jetty-util-8.1.2.v20120308.jar</Set>

Had to hardcode the Jetty install dir, since my Jetty is installed outside of Eclipse and doing the <SystemProperty name="jetty.home" default="."/> resolves to the jetty home under .metadata.