Java Error(一)

1、Resource  leak: 'xxx' is never closed

原因:打开了reader 、Scanner等,未关闭,导致内存泄漏。

解决方案:

public void readShapeData() throws IOException {
    Scanner in = new Scanner(System.in);
    try {
        System.out.println("Enter the width of the Rectangle: ");
        width = in.nextDouble();
        System.out.println("Enter the height of the Rectangle: ");
        height = in.nextDouble();
    } 
  finally {
        in.close();
    }
}

使用try{}  finally{} block来解决。try{}中可能会抛出异常的part,用于测试。finally{}中为不管怎样,最终都要执行的步骤。类似Python,或者是Python的开发人员保留了Java这一特征。

避免了因Logic Flaw 导致一直调用内存资源,导致内存资源不足。

2、jdk 安装环境,cmd中输入javac   类名.java 显示错误

错误: 找不到或无法加载主类 HelloWrold  原因: java.lang.ClassNotFoundException: HelloWorld(找不到HelloWorld 该类 )

1、没有定义public class 类,如class HelloWorld{ ....}

2、public class 类名与文件名书写有出入,大小写、符号等等。

原文地址:https://www.cnblogs.com/leafh/p/8581750.html