开发者社区> 问答> 正文

jdk1.7 下当代码仅多加一个try-catch时就会报错,不知道为什么?报错

Environment: jdk1.7
javax.servlet-api-3.0.1.jar is needed for this test.
Reproduce steps:
javac Test1.java -cp javax.servlet-api-3.0.1.jar build Test1.java with javax.servlet-api-3.0.1.jar

javac Test2.java -cp javax.servlet-api-3.0.1.jar build Test2.java with javax.servlet-api-3.0.1.jar

javac Test3.java build Test3.java
But when this command java Test3 run, Exception is thrown. The result at the end of this post.
Test1.hello()运行正常,而Test2.hello()运行失败
Test1.java
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
public class Test1 {
    public void getRequest(HttpServletResponse resp) throws IOException {
        OutputStream os = resp.getOutputStream();
        resp.getOutputStream().close();
    }
    public void hello(String world) {
        System.out.println("hello " + world);
    }
}
Test2.java
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
public class Test2 {
    public void getRequest(HttpServletResponse resp) throws IOException {
        OutputStream os = resp.getOutputStream();
        try {
            resp.getOutputStream().close();
        } catch (Exception e) {
        }
    }
    public void hello(String world) {
        System.out.println("hello " + world);
    }
}
Test3.java
public class Test3 {
    public static void main(String[] args) {
        new Test1().hello("world1");
        new Test2().hello("world2");
    }
}
output of the final step. Test2.hello("world2") throw an exception:
hello world1
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStream
    at cn.test.abc1.Test3.main(Test3.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletOutputStream
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 6 more
I am very confused with the Exception. Because I didn't use any code in the Class ServletOutputStream. And the difference of the Test1 and Test2 is only a try blocked.

展开
收起
爱吃鱼的程序员 2020-06-09 11:59:07 747 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    和try有什么关系。

    报错提示看不懂么.

    Exceptioninthread"main"java.lang.NoClassDefFoundError:javax/servlet/ServletOutputStream
    jar里面没有这个类。为什么Test1.hello()运行不报错,而Test2.hello()报错 你修改成
    throwsException

    试试看

    你修改成
    throwsException

    试试看

    一点建议。
    你catch住的是一个Exception。而你抛出的是一个IOException,Exception是所有异常的父类。catch不住的才会抛出,但Exception的话,会catch住所有的异常。ThrowsIOException其实是走不到的。

    正常的话应该是这样的:

    publicvoidgetRequest(HttpServletResponseresp)throwsException{
    。。。

            if(os!=null){

          try{

             os.close();

            }catch(IOExceptionioe){

              //donothing.

            }

        }

    }


    os

    2020-06-09 11:59:26
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载