'java.lang.ClassNotFoundException: org.eclipse.jetty.Server

I am trying to run a maven project when I run mvn install everything is ok I got build success qnd when I tried to lunch the project I got the following exception:

Exception in thread "main" java.lang.ClassNotFoundException: org.eclipse.jetty.Server
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.eclipse.jetty.util.Loader.loadClass(Loader.java:86)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.nodeClass(XmlConfiguration.java:364)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:292)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:248)
at net.es.oscars.wbui.http.WebApp.main(WebApp.java:44)

and when I run mvn dependency:tree I got the following results:

     maven-dependency-plugin:2.8:tree (default-cli) @ wbui ---
     [INFO] net.es.oscars:wbui:jar:0.0.1-SNAPSHOT
     [INFO] +- net.es.oscars:utils:jar:0.0.1-SNAPSHOT:compile
     [INFO] |  +- net.perfsonar:ps-api:jar:0.5:compile
     [INFO] |  |  +- commons-httpclient:commonhttpclient:jar:3.0.1:compile
     [INFO] |  |  |  +- junit:junit:jar:3.8.1:compile
     [INFO] |  |  |  \- commons-codec:commons-codec:jar:1.2:compile
     [INFO] |  |  +- org.jdom:jdom:jar:1.1:compile
     [INFO] |  |  +- org.jdom:jaxen-jdom:jar:1.0-FCS:compile
     [INFO] |  |  +- org.jdom:jaxen-core:jar:1.0-FCS:compile
     [INFO] |  |  \- org.jdom:saxpath:jar:1.0-FCS:compile
     [INFO] |  +- org.jyaml:jyaml:jar:1.3:compile
     [INFO] |  +- log4j:log4j:jar:1.2.17:compile
     [INFO] |  +- net.es.oscars:common-logging:jar:0.0.1-SNAPSHOT:compile
     [INFO] |  |  \- gov.lbl:netlogger:jar:4.1.2:compile
     [INFO] |  +- net.es.oscars:common-soap:jar:0.0.1-SNAPSHOT:compile
     [INFO] |  +- org.hibernate:hibernate-core:jar:3.3.1.GA:compile
     [INFO] |  |  +- antlr:antlr:jar:2.7.6:compile
      [INFO] |  |  +- commons-collections:commons-collections:jar:3.1:compile
     [INFO] |  |  \- dom4j:dom4j:jar:1.6.1:compile
     [INFO] |  |     \- xml-apis:xml-apis:jar:1.0.b2:compile
     [INFO] |  +- org.mortbay.jetty:jetty:jar:6.1.16:compile
     [INFO] |  |  +- org.mortbay.jetty:jetty-util:jar:6.1.16:compile
     [INFO] |  |  \- org.mortbay.jetty:servlet-api:jar:2.5-20081211:compile
     [INFO] |  +- org.opensymphony.quartz:quartz:jar:1.6.1:compile
     [INFO] |  +- org.apache.ws.security:wss4j:jar:1.5.8:compile
     [INFO] |  |  +- commons-logging:commons-logging:jar:1.1:compile
     [INFO] |  |  |  +- logkit:logkit:jar:1.0.1:compile
     [INFO] |  |  |  +- avalon-framework:avalon-framework:jar:4.1.3:compile
     [INFO] |  |  |  \- javax.servlet:servlet-api:jar:2.3:compile
     [INFO] |  |  +- org.apache.santuario:xmlsec:jar:1.4.3:compile
     [INFO] |  |  \- xalan:xalan:jar:2.7.1:compile
    [INFO] |  |     \- xalan:serializer:jar:2.7.1:compile
    [INFO] |  +- org.apache.cxf:cxf-rt-bindings-soap:jar:2.2.5:compile
    [INFO] |  |  +- org.apache.cxf:cxf-api:jar:2.2.5:compile
     [INFO] |  |  |  +- org.apache.geronimo.specs:geronimo-activation_1.1_spec:jar:1.0.2:compile
    [INFO] |  |  |  +- org.apache.ws.commons.schema:XmlSchema:jar:1.4.5:compile
    [INFO] |  |  |  +- org.apache.geronimo.specs:geronimo-annotation_1.0_spec:jar:1.1.1:compile
    [INFO] |  |  |  +- org.codehaus.woodstox:wstx-asl:jar:3.2.9:compile
    [INFO] |  |  |  +- org.apache.neethi:neethi:jar:2.0.4:compile
    [INFO] |  |  |  \- org.apache.cxf:cxf-common-schemas:jar:2.2.5:compile
    [INFO] |  |  +- org.apache.cxf:cxf-tools-common:jar:2.2.5:compile
    [INFO] |  |  |  +- wsdl4j:wsdl4j:jar:1.6.2:compile
    [INFO] |  |  |  \- com.sun.xml.bind:jaxb-impl:jar:2.1.12:compile
    [INFO] |  |  +- org.apache.cxf:cxf-rt-databinding-jaxb:jar:2.2.5:compile
   [INFO] |  |  |  \- org.apache.geronimo.specs:geronimo-javamail_1.4_spec:jar:1.6:compile
    [INFO] |  |  \- javax.xml.bind:jaxb-api:jar:2.1:compile
    [INFO] |  +- org.apache.cxf:cxf-rt-ws-security:jar:2.2.5:compile
    [INFO] |  |  +- org.apache.cxf:cxf-rt-core:jar:2.2.5:compile
    [INFO] |  |  |  \- org.springframework:spring-core:jar:2.5.5:compile
    [INFO] |  |  +- org.apache.cxf:cxf-common-utilities:jar:2.2.5:compile
    [INFO] |  |  |  +- org.springframework:spring-beans:jar:2.5.5:compile
    [INFO] |  |  |  +- org.springframework:spring-context:jar:2.5.5:compile
    [INFO] |  |  |  |  \- aopalliance:aopalliance:jar:1.0:compile
    [INFO] |  |  |  +- org.apache.geronimo.specs:geronimo-stax-api_1.0_spec:jar:1.0.1:compile
    [INFO] |  |  |  \- commons-lang:commons-lang:jar:2.4:compile
    [INFO] |  |  +- javax.xml.soap:saaj-api:jar:1.3:compile
    [INFO] |  |  \- org.bouncycastle:bcprov-jdk15:jar:1.43:runtime
    [INFO] |  +- org.apache.cxf:cxf-rt-frontend-jaxws:jar:2.2.5:compile
    [INFO] |  |  +- xml-resolver:xml-resolver:jar:1.2:compile
    [INFO] |  |  +- org.apache.geronimo.specs:geronimo-jaxws_2.1_spec:jar:1.0:compile
    [INFO] |  |  +- org.apache.geronimo.specs:geronimo-ws-metadata_2.0_spec:jar:1.1.2:compile
    [INFO] |  |  +- asm:asm:jar:2.2.3:compile
    [INFO] |  |  +- org.apache.cxf:cxf-rt-bindings-xml:jar:2.2.5:compile
    [INFO] |  |  +- org.apache.cxf:cxf-rt-frontend-simple:jar:2.2.5:compile
    [INFO] |  |  +- org.apache.cxf:cxf-rt-ws-addr:jar:2.2.5:compile
    [INFO] |  |  \- com.sun.xml.messaging.saaj:saaj-impl:jar:1.3.2:compile
    [INFO] |  +- org.apache.cxf:cxf-rt-transports-http:jar:2.2.5:compile
    [INFO] |  |  \- org.springframework:spring-web:jar:2.5.5:compile
    [INFO] |  \- org.apache.cxf:cxf-rt-transports-http-jetty:jar:2.2.5:compile
    [INFO] |     \- org.apache.geronimo.specs:geronimo-servlet_2.5_spec:jar:1.2:compile
   [INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
   [INFO] +- net.es.oscars:oscars-war:war:0.0.1-SNAPSHOT:provided
   [INFO] +- org.slf4j:slf4j-api:jar:1.7.7:compile
   [INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.7:compile
   [INFO] +- org.eclipse.jetty:jetty-server:jar:9.3.0.M0:compile
   [INFO] |  +- org.eclipse.jetty:jetty-http:jar:9.3.0.M0:compile
   [INFO] |  |  \- org.eclipse.jetty:jetty-util:jar:9.3.0.M0:compile
   [INFO] |  \- org.eclipse.jetty:jetty-io:jar:9.3.0.M0:compile
   [INFO] +- org.eclipse.jetty:jetty-webapp:jar:9.3.0.M0:compile
   [INFO] |  \- org.eclipse.jetty:jetty-xml:jar:9.3.0.M0:compile
   [INFO] \- org.eclipse.jetty:jetty-servlet:jar:9.3.0.M0:compile
    [INFO]    \- org.eclipse.jetty:jetty-security:jar:9.3.0.M0:compile
   [INFO]   ------------------------------------------------------------------------
   [INFO] BUILD SUCCESS

and this is my pom.xml file :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>wbui</artifactId>
<name>OSCARS WBUI</name>

<parent>
    <groupId>net.es.oscars</groupId>
    <artifactId>OSCARS</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
    <jetty.version>9.3.0.M0</jetty.version>
</properties>


<!-- puts all code & libraries in one jar -->
<pluginRepositories>
    <pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
    </pluginRepository>
</pluginRepositories>

<dependencies>
    <!-- OSCARS utilities (from local repository) -->
    <dependency>
        <groupId>net.es.oscars</groupId>
        <artifactId>utils</artifactId>
        <version>${oscars.version}</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <!-- kludge to get dependency on local module oscars-war without
         including war file -->
    <dependency>
        <groupId>net.es.oscars</groupId>
        <artifactId>oscars-war</artifactId>
        <version>${oscars.version}</version>
        <type>war</type>
        <scope>provided</scope>
    </dependency>



    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.7</version>
    </dependency>



    <!--  Jetty -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.3.0.M0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>9.3.0.M0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.3.0.M0</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <!-- puts all code & libraries in one jar -->

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <!-- Run shade goal on package phase -->
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <shadedClassifierName>one-jar</shadedClassifierName>
                        <shadedArtifactAttached>true</shadedArtifactAttached>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <artifactSet>
                            <excludes>
                                <exclude>org.apache.geronimo.specs:geronimo-servlet_2.5_spec</exclude>
                                <exclude>org.mortbay.jetty:servlet-api</exclude>
                                <exclude>javax.servlet:servlet-api</exclude>
                            </excludes>
                        </artifactSet>
                        <transformers>
                            <!-- add Main-Class to manifest file -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>net.es.oscars.wbui.http.WebApp</mainClass>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/services/com.sun.tools.xjc.Plugin</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/cxf/cxf.extension</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/extensions.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/cxf/extensions.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/cxf/bus-extensions.txt</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/cxf/bus-extensions.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/wsdl.plugin.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/tools.service.validator.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.cxf.maven.PluginTransformer">
                                <resource>META-INF/tools-plugin.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/cxf/java2wsbeans.xml</resource>
                            </transformer>
                            <transformer implementation="org.apache.cxf.maven.CXFAllTransformer" />
                        </transformers>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-buildtools</artifactId>
                    <version>2.2.12</version>
                    <type>jar</type>
                    <scope>compile</scope>
                </dependency>
            </dependencies>
        </plugin>
        <!-- specifically use java 1.5 to compile -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

     package net.es.oscars.wbui.http;

   import java.util.Map;
   import java.io.File;
   import java.io.FileInputStream;

  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.webapp.WebAppContext;
  import org.eclipse.jetty.xml.XmlConfiguration;

 import net.es.oscars.utils.config.*;
 import net.es.oscars.utils.svc.ServiceNames;

 public class WebApp {
public static ContextConfig cc = null;

public static void main(String[] args) throws Exception {

    cc = ContextConfig.getInstance(ServiceNames.SVC_WBUI);
    cc.setServiceName(ServiceNames.SVC_WBUI);
    String context = "PRODUCTION";

    if (args.length > 0) {
        context = args[1];
    }
    String warFile = null;
    String warTempDir = null;
    Server server = new Server();

    cc.setContext(context);
    System.setProperty("context",context);
    System.out.println("starting WBUI with context "+ context);
    try {
        cc.loadManifest(ServiceNames.SVC_WBUI,  ConfigDefaults.MANIFEST); // manifest.yaml
        cc.setLog4j();

        String configFile = cc.getFilePath(ConfigDefaults.CONFIG);
        Map config = ConfigHelper.getConfiguration(configFile);
        Map http = (Map) config.get("http");
        warFile = (String) http.get("warFile");
        warTempDir = (String) http.get("warTempDir");
        String jettyConf = cc.getFilePath("jetty.xml");
        XmlConfiguration configuration = new XmlConfiguration(new FileInputStream(jettyConf));
        configuration.configure(server);
    } catch (ConfigException ex) {
        System.out.println("caught ConfigurationException " + ex.getMessage());
        System.exit(-1);
    }
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/OSCARS");
    webapp.setWar(warFile);
    if(warTempDir != null && !warTempDir.equals("")){
        webapp.setTempDirectory(new File(warTempDir));
    }
    server.setHandler(webapp);
    server.start();
    server.join();
}
 }

so any idea how to fix this problem !!!!



Solution 1:[1]

Your dependency:tree has an ancient version of Jetty.

org.mortbay.jetty:jetty:jar:6.1.16:compile

That's not going to work.

You'll need to fix net.es.oscars:utils:jar to not use that ancient, long since EOL (End of Life) version of Jetty.

Your pom.xml has a reference to an unstable milestone release

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>9.3.0.M0</version>
</dependency>

(The M0 section in the version means "Milestone 0")

Use a stable release version of Jetty.

Look at https://www.eclipse.org/jetty/download.html to see what's stable (milestone releases are never listed there)

Also look at https://github.com/eclipse/jetty.project/releases

Also see the Jetty Documentation for the overall list of major jetty versions. https://www.eclipse.org/jetty/documentation/current/what-jetty-version.html

As of the time of this answer, the latest stable release version is 9.4.18.v20190429

As for the java.lang.ClassNotFoundException, you'll need to fix your dependencies first. If the problem still persists after fixing your dependencies, consider posting how you create your classloaders, their hierarchy, and how you use XmlConfiguration (and from which classloader).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1