大写和NoClassDefFoundError与ClassNotFoundException [英] Capitalization and NoClassDefFoundError vs ClassNotFoundException

查看:130
本文介绍了大写和NoClassDefFoundError与ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到跨平台的差异有关Class.forName()何时抛出ClassNotFoundException以及何时抛出NoClassDefFoundError。这种行为在某处定义得很好,或者我偶然发现了一个错误?

I'm seeing differences across platforms about when Class.forName() throws ClassNotFoundException and when it throws NoClassDefFoundError. Is this behavior well-defined somewhere, or have I stumbled across a bug?

考虑以下代码(默认包中的独立java文件):

Consider the following code (which is a standalone java file in the default package):

public class DLExceptionType {

  private static void printFindError(String name) {
    System.out.print(name + ": ");
    try {
      Class.forName(name);
      System.out.println("** no error **");
    } catch (Throwable e) {
      System.out.println(e);
    }
  }

  public static void main(String[] args) {
    printFindError("DLExceptionType");
    printFindError("dLExceptionType"); // note the mis-capitalization
  }
}

代码生成Linux上的预期输出:

The code produces the expected output on Linux:

[eos18:~]$ java -version DLExceptionType
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)
[eos18:~]$ java DLExceptionType
DLExceptionType: ** no error **
dLExceptionType: java.lang.ClassNotFoundException: dLExceptionType

它在Windows上生成一个不同但可理解的输出:

It produces a different, but understandable, output on Windows:

java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) Client VM (build 21.1-b02, mixed mode, sharing)

Y:\Temp>java DLExceptionType
DLExceptionType: ** no error **
dLExceptionType: java.lang.NoClassDefFoundError: dLExceptionType (wrong name:  DLExceptionType)

o Windows上的utput是有意义的:因为文件系统不区分大小写,所以JVM加载文件dLExceptionType.class,但该文件包含一个具有不同名称的类:DLExceptionType

The output on Windows makes sense: Because the file system is not case sensitive, the JVM loads the file dLExceptionType.class, but that file contains a class with a different name: DLExceptionType

但是,当我在Mac上运行代码时(有一个区分大小写的文件系统和比Linux盒子更新的JVM)我获得与Windows相同的输出:

However, when I run the code on Mac (with has a case-sensitive file system and a newer JVM than the Linux box) I get the same output as Windows:

$ java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-10M3527)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)
$ java DLExceptionType
DLExceptionType: ** no error **
dLExceptionType: java.lang.NoClassDefFoundError: dLExceptionType (wrong name: DLExceptionType)


推荐答案

HFS +(Mac Extended)通常不区分大小写。由于Mac OS 10.3 Apple推出了HFSX,它可以区分大小写(但不是默认设置)。如果您没有通过磁盘初始化指定该选项,那么您的卷很可能不区分大小写。

HFS+ (Mac Extended) is usually not case sensitive. Since Mac OS 10.3 Apple introduced HFSX which can be case sensitive (but is not the default). If you did not specify the option by the disk initialization then your volume is most likely case insensitive.

请参阅: http://en.wikipedia.org/wiki/HFS_Plus

这篇关于大写和NoClassDefFoundError与ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆