Servlet异常:方法抛出“ java.lang.NoClassDefFoundError”

时间:2019-05-01 12:31:52

标签: java exception java-ee

我正在使用此库:Java EE Maven项目上的https://github.com/bertrandmartel/speed-test-lib

我遇到了以下异常java.lang.ClassNotFoundException: fr.bmartel.speedtest.inter.ISpeedTestListener from [Module "deployment.vp_speedometer_war_exploded.war:main" from Service Module Loader]

我创建了此类:

 public class SpeedTest {

    public SpeedTest() {
        speedTestSocket = new SpeedTestSocket();
    }

    public void start() {
        //set timeout for download
        speedTestSocket.setSocketTimeout(SOCKET_TIMEOUT);

        // add a listener to wait for speed examples completion and progress
        speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {
            public void onProgress(final float percent, final SpeedTestReport downloadReport) {
                transfertRateBitSecond = downloadReport.getTransferRateBit(); // + " bit/second";
            }
        });

        speedTestSocket.startDownload(SPEED_TEST_SERVER_URI_DL);
    }

我的Servlet看起来像:

public class Servlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        SpeedTest speedTest = new SpeedTest();
        speedTest.start();

        session.setAttribute("nbr", speedTest.getTransfertRateBitSecond());
        response.sendRedirect("index.jsp");
    }
}

它在SpeedTest speedTest = new SpeedTest();处引发了异常 但是我找不到解决方法...

root cause

java.lang.NoClassDefFoundError: fr/bmartel/speedtest/inter/ISpeedTestListener
    ctrl.Servlet.doGet(Servlet.java:22)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

root cause

java.lang.ClassNotFoundException: fr.bmartel.speedtest.inter.ISpeedTestListener from [Module "deployment.vp_speedometer_war_exploded.war:main" from Service Module Loader]
    org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
    org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
    org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
    org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
    org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
    ctrl.Servlet.doGet(Servlet.java:22)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

感谢您的帮助


修改

这是我的pom.xml

<groupId>ch</groupId>
<artifactId>speedometer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <!-- https://github.com/bertrandmartel/speed-test-lib -->
    <dependency>
        <groupId>fr.bmartel</groupId>
        <artifactId>jspeedtest</artifactId>
        <version>1.32.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <webXml>web\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

构建项目时,在“ war”中有我的“ jspeedtest-1.13.1.jar”库。没关系

2 个答案:

答案 0 :(得分:0)

ISpeedTestListenerinterface,您需要实现其所有方法:

speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {

        @Override
        public void onCompletion(SpeedTestReport report) {
            // do nothing
        }

        @Override
        public void onError(SpeedTestError speedTestError, String 
          errorMessage) {
            // do nothing
        }

        @Override
        public void onProgress(float percent, SpeedTestReport report) {
            transfertRateBitSecond = downloadReport.getTransferRateBit(); // + " bit/second";

        }
  });

答案 1 :(得分:0)

我觉得问题正在发展。当我尝试访问我的应用程序时,我得到了...

  13:04:21,652 WARN  [org.jboss.modules] (MSC service thread 1-15) Failed to define class ctrl.Servlet in Module "deployment.speedometer-1.0-SNAPSHOT.war:main" from Service Module Loader: java.lang.UnsupportedClassVersionError: ctrl/Servlet : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method) [rt.jar:1.7.0_79]
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800) [rt.jar:1.7.0_79]
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) [rt.jar:1.7.0_79]
    at org.jboss.modules.ModuleClassLoader.doDefineOrLoadClass(ModuleClassLoader.java:327)