- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
如果一个人在不同的目录中使用相同的不区分大小写的名称编写两个公共(public) Java 类,那么这两个类在运行时都不能使用。 (我在 Windows、Mac 和 Linux 上使用多个版本的 HotSpot JVM 进行了测试。如果有其他 JVM 可以同时使用它们,我不会感到惊讶。)例如,如果我创建一个名为 a
的类> 和一个像这样命名的 A
:
// lowercase/src/testcase/a.java
package testcase;
public class a {
public static String myCase() {
return "lower";
}
}
// uppercase/src/testcase/A.java
package testcase;
public class A {
public static String myCase() {
return "upper";
}
}
包含上述代码的三个 Eclipse 项目是 available from my website .
如果我尝试在两个类上调用 myCase
,如下所示:
System.out.println(A.myCase());
System.out.println(a.myCase());
类型检查成功,但是当我运行上面直接生成的代码时,我得到:
Exception in thread "main" java.lang.NoClassDefFoundError: testcase/A (wrong name: testcase/a)
在 Java 中,名称通常区分大小写。一些文件系统(例如 Windows)不区分大小写,所以我对上述行为的发生并不感到惊讶,但它似乎错误。不幸的是,Java 规范对于哪些类是可见的很奇怪。 Java Language Specification (JLS), Java SE 7 Edition (第 6.6.1 节,第 166 页)说:
If a class or interface type is declared public, then it may be accessed by any code, provided that the compilation unit (§7.3) in which it is declared is observable.
在第 7.3 节中,JLS 用极其模糊的术语定义了编译单元的可观察性:
All the compilation units of the predefined package java and its subpackages lang and io are always observable. For all other packages, the host system determines which compilation units are observable.
Java Virtual Machine Specification同样含糊不清(第 5.3.1 节):
The following steps are used to load and thereby create the nonarray class or interface C denoted by [binary name] N using the bootstrap class loader [...] Otherwise, the Java virtual machine passes the argument N to an invocation of a method on the bootstrap class loader to search for a purported representation of C in a platform-dependent manner.
所有这些都导致了四个问题,按重要性降序排列:
a
和 A
?编写自定义类加载器有用吗?最佳答案
- Are there any guarantees about which classes are loadable by the bootstrap class loader in every JVM?
语言的核心部分,以及支持的实现类。不保证包含您编写的任何类(class)。 (普通的 JVM 将您的类加载到与 Bootstrap 不同的类加载器中,事实上,普通的引导加载器通常从 JAR 中加载其类,因为这比充满类的大型旧目录结构更有效地部署。)
- If there are any guarantees, does the behavior in the example above violate the guarantee (i.e. is the behavior a bug)?
- Is there any way to make "standard" JVMs load a and A simultaneously? Would writing a custom class loader work?
Java 通过将类的全名映射到文件名来加载类,然后在类路径中搜索该文件名。因此 testcase.a
转到 testcase/a.class
并且 testcase.A
转到 testcase/A.class
.一些文件系统将这些东西混合在一起,并且可能会在需要时为其他文件系统提供服务。其他人做对了(特别是 JAR 文件中使用的 ZIP 格式的变体是完全区分大小写和可移植的)。 Java 对此无能为力(尽管 IDE 可以通过将 .class
文件远离 native FS 来为您处理它,但我不知道是否有任何实际操作和 JDK 的 javac
肯定没那么聪明)。
但这并不是这里要注意的唯一一点:类文件在内部知道它们在谈论什么类。文件中缺少 expected 类仅意味着加载失败,导致您收到 NoClassDefFoundError
。你得到的是一个问题(至少在某种意义上是错误部署),它被检测到并得到了强有力的处理。从理论上讲,您可以构建一个可以通过不断搜索来处理此类事情的类加载器,但是何必呢?这些都得到了正确的处理。
更一般地说,如果您经常遇到此问题,请在具有区分大小写文件系统的 Unix 上进行生产构建(推荐使用 Jenkins 等 CI 系统)并查找哪些开发人员是仅区分大小写并让它们停止,因为这非常令人困惑!
关于java - Java 类名区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10890805/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
出于某种原因,右栏中的精选文章忽略了“#elementtext”和“#elementtext:hover”属性。仅显示“p.element”和“img.element”。 有什么想法吗? 谢谢 - 塔
我有两个值,每个值都来自不同的枚举。我想检查这两者的允许组合,如果没有找到则执行默认操作。我能以某种方式对这两个值进行切换/大小写吗?我想避免使用多个 if/else 语句或遵循位掩码模式的枚举,只是
我需要 where 但 not 大小写。例如,我想找到没有名字“莎士比亚”的戏剧: _.where(listOfPlays, {author: !"Shakespeare", year: 1611})
我想实现一个 parking 场应用所以有一个带5个或更多 parking 位的车库当司机 parking 时,车库中的下一个空闲位置应该分配给他。 所以我有一个带 5 个或更多插槽的 table 上
我想使用 Erlang 来确定传递给函数的变量是否可以被数字整除。我考虑过使用 case 来执行此操作,但是我找不到解决方案。 case 是适合这项工作的工具吗? 示例:将数字传递给函数 f()。如果
我在 phpmyadmin 中创建了一个表,其列名如 first_name、last_name。当我使用命令显示表中的列名时,它会将它们显示为 first_name。 我想显示我的列名称,如 Firs
使用 Swift 4,如何使用这些规则格式化字符串: 如果单词超过 3 个字母,则首字母大写,否则大写 包含像 St-Michel 这样的连字符的 Pascal 大小写单词 我这里有初稿,但我一直在思
这个问题在这里已经有了答案: Why can't the switch statement be applied to strings? (23 个回答) 关闭 8 年前。 大家好 所以我正在尝试对
在 MVC 操作中,我如何访问使用多个同名值提交的“表单数据”中的值? 我做了什么:int、decimal、string 类型的值工作完美。 问题:每个变体都有一个复选框,所以当我尝试获取它时,它只显
while(1) { char buff[1000]; printf("Enter the word: "); fgets(buff, 1000
我有一个 Dllmain,它在线程附加到此 DLL 时分配线程本地存储。代码如下: BOOL APIENTRY DllMain(HMODULE hModule,
我有一个变量名,比如“WARD_VS_VITAL_SIGNS”,我想将它转换为 Pascal 大小写格式:“WardVsVitalSigns” WARD_VS_VITAL_SIGNS -> WardV
我是 Swift 编码的新手,正在尝试弄清楚如何在触摸节点时制作具有开/关功能的循环音频。我认为实现它的最佳方式是通过 SKAudioNode,但我不确定我在以下代码中做错了什么。当在节点上按下时 -
这是我第一次使用这种枚举,具有关联值类型的枚举,我需要根据对象的类型制作一个 switch 语句,我无法做到,这是枚举: enum TypeEnum { case foo(FooClass)
我想从字符串中删除所有下划线,下划线后面的字符为大写。因此,例如:_my_string_ 变为:MyString 同样:my_string 变为 MyString 有没有更简单的方法呢?我目前有以下内
如何在 Java 中将蛇形大小写转换为 Camel 形大小写? 输入:“input_in_snake_case” 输出:“InputInSnakeCase” 最佳答案 Guava通过其CaseForm
我们有一个表auth_group_access,那么如何使用呢? 在使用M方法时,对于带下划线的表名,可以采用如下方法。 M('AuthGroupAccess'); 对应sql语句SQL: S
我正在制作一个 pygame 游戏,每当我运行我的代码时,我都会收到错误 expected ':'。我知道在 match/case block 中使用 [ 和 ] 用于其他用途,但我该如何解决这个问题
有人能告诉我是否可以使用正则表达式将 url 转换为小写? 这是在 html img 标签内,所以我们可以通过标签找到网址。 这是我所拥有的一个例子 我需要在最后小写图像名称。 该文档包含更多 H
我是一名优秀的程序员,十分优秀!