eclipse Java 区分大小写:类文件冲突:存在不同大小写的资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25810791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Case Sensitivity: Class file collision: A resource exists with a different case
提问by Vishrant
I am in Windows OS, working in eclipse IDE, I have following two scenario:
我在 Windows 操作系统中,在 eclipse IDE 中工作,我有以下两种情况:
1. I have created below class, this is in Employee.java class (E is in uppercase):
1.我创建了下面的类,这是在Employee.java类中(E是大写的):
public class Employee {
public static void main(String[] args) {
employee emp = new employee();
emp.test();
}
}
//here e is in lowercase
class employee {
public void test() {
System.out.println("I am in test()");
}
}
In this case I got below Exception:
在这种情况下,我得到以下异常:
Exception in thread "main" java.lang.NoClassDefFoundError: employee (wrong name: Employee)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
.
.
.
at Employee.main(Employee.java:4) // i.e at emp.test();
2. I have created below class, this is Employee.java class:
2. 我创建了下面的类,这是 Employee.java 类:
public class Employee {
public static void main(String[] args) {
}
}
Now when I tried to create employee.java (e is in lower case) class in same package, I got the message in eclipse that Type with same name but different case exists.
现在,当我尝试在同一个包中创建employee.java(e 为小写)类时,我在 eclipse 中收到了消息 Type with same name but different case exists.
My question is: Java is case sensitive. Does this means JVM (windows and UNIX based) is case sensitive or the compiler is case sensitive? And why it gives that exception in scenario 1 and eclipse does not allow me to create employee.java file in same location in scenario 2.
我的问题是:Java 区分大小写。这是否意味着 JVM(基于 Windows 和 UNIX)区分大小写或编译器区分大小写?以及为什么它在场景 1 中给出该异常,而 Eclipse 不允许我在场景 2 中的相同位置创建employee.java 文件。
Also remember I am on Windows OS which is case insensitive for file name, so I know that it will not allow employee.java and Employee.java in same location. Does this violate in any means Java case sensitivity?
还记得我在 Windows 操作系统上,它对文件名不区分大小写,所以我知道它不允许在同一位置使用 employee.java 和 Employee.java。这是否以任何方式违反 Java 区分大小写?
回答by Joop Eggen
The file system on Windows is not case-sensitive: Employee.class and employee.class cannot reside in the same directory.
Windows 上的文件系统不区分大小写:Employee.class 和 employee.class 不能驻留在同一目录中。
Scenario 1 with both classes defined in the same java source:The compiler creating employee.class probably overwerites Employee.class or whatever.
在同一个 java 源代码中定义两个类的场景 1:创建employee.class 的编译器可能会覆盖Employee.class 或其他任何东西。
Scenario 2 with separate java sources:Eclipse would not be able to create an employee.java in the same directory as Employee.java (under Windows file system). Hence someone in the past took care to handle the error user friendly.
使用单独的 java 源的场景 2:Eclipse 将无法在与 Employee.java 相同的目录中创建employee.java(在 Windows 文件系统下)。因此,过去有人负责处理用户友好的错误。
Java assumes case-sensitive names.
Java 采用区分大小写的名称。
You could take your sources to Linux, compile them to a .jar (a zip format, again case-sensitive). Then copy the jar to windows and execute it.
您可以将源代码带到 Linux,将它们编译为 .jar(zip 格式,同样区分大小写)。然后将 jar 复制到 windows 并执行它。
By the way, I think you have found a new way to obfuscating java source code, decompiling under Windows.
顺便说一句,我认为您已经找到了一种混淆java源代码的新方法,在Windows下进行反编译。
A related topic is using Unicode letters in class / file names. That touches different platforms, different canonical representations of Unicode (é
as one or two Unicode code points), version control systems.
一个相关的主题是在类/文件名中使用 Unicode 字母。这涉及不同的平台、Unicode 的不同规范表示(é
作为一两个 Unicode 代码点)、版本控制系统。
Elaboration:(for the interested)
详细说明:(有兴趣者)
The human character é
has two representations in Unicode:
人类字符é
在 Unicode 中有两种表示:
- "\u00E9" - as one code point U+00E9,
é
; - "e\u0301" - as two code points, U+009B, letter
e
, plus combining diacritical mark U-0301,′
(zero width accent).
- "\u00E9" - 作为一个代码点 U+00E9,
é
; - "e\u0301" - 作为两个代码点,U+009B,字母
e
,加上组合变音符号 U-0301,′
(零宽度重音)。
Unfortunately different operating systems (I was told) use a different canonical representation. I once wanted to use the version control system hgbut had to find out, their missing support for Linux/Windows interoperability.
不幸的是,不同的操作系统(有人告诉我)使用不同的规范表示。我曾经想使用版本控制系统hg但不得不找出它们缺少对 Linux/Windows 互操作性的支持。
Otherwise one could normalize to a canonical form with java.text.Normalizer
.
否则,可以使用 规范化为规范形式java.text.Normalizer
。
So let's wait for:
那么让我们拭目以待:
class CaféMa?anaFa?ade
回答by Ian Roberts
This isn't a limitation of Java but of the Windows file system where you're trying to write the files. On a case-insensitive file system the compiler can't write both Employee.class
and employee.class
to disk because as far as the FS is concerned they are the same file.
这不是 Java 的限制,而是您尝试写入文件的 Windows 文件系统的限制。在不区分大小写的文件系统上,编译器不能同时写入Employee.class
和employee.class
到磁盘,因为就 FS 而言,它们是同一个文件。
If you could somehow do the compilation to a case-sensitive file system (e.g. on Linux) and then package the resulting class files into a JAR then you would be able to run from that JAR without error on Windows, as a ZIP file isable to contain two distinct entries whose names differ only in case.
如果你能以某种方式做编译来区分大小写的文件系统(例如在Linux上),然后打包生成的类文件到一个JAR,那么你将能够从该JAR没有Windows错误运行,作为一个ZIP文件是能够包含两个不同的条目,其名称仅在大小写上有所不同。
回答by Dave Newton
No, it doesn't; Java is case-sensitive, and that's all Java has control over–itself.
不,它没有;Java 区分大小写,这就是 Java 对自身的控制。
It cannot control your file system, the implementation of which might not respect Java policies.
它无法控制您的文件系统,该文件系统的实现可能不遵守 Java 策略。
回答by Gauthier JACQUES
Anyway, by convention, class names should always start with an upper case.
无论如何,按照惯例,类名应该总是以大写开头。
This convention helps avoiding the problem you're describing.
此约定有助于避免您所描述的问题。
回答by James Jithin
I had this issue with a gradle project which contained multiple children projects and the sources were different. One of them had a class that read com.package.level1.Main
and another one had com.package.level1.main.Util
. When I ran gradle build for the entire project, it ran well from command line, but the red exclamation on Eclipse project was tough enough to solve. How got rid of this was to remove the auto Java compiling of source files for the project and set to use only gradle. Here is how it is:
我在一个包含多个子项目且来源不同的 gradle 项目中遇到了这个问题。其中一个有一个阅读类,com.package.level1.Main
另一个有com.package.level1.main.Util
。当我为整个项目运行 gradle build 时,它从命令行运行良好,但是 Eclipse 项目上的红色感叹号很难解决。如何摆脱这个是删除项目源文件的自动Java编译并设置为仅使用gradle。这是它的样子:
- Go to
Project Properties
. - Choose
Builders
from the left pane. - Unselect
Java Builder
and keep onlyGradle Project Builder
. - Apply and Close the properties.
- Delete the project from workspace. While deleting, choose the option not to delete the files from filesystem.
- Import the project back to Eclipse and you should be done.
- 去
Project Properties
。 Builders
从左窗格中选择。- 取消选择
Java Builder
并只保留Gradle Project Builder
。 - 应用和关闭属性。
- 从工作区中删除项目。删除时,选择不从文件系统中删除文件的选项。
- 将项目导入回 Eclipse,您应该就完成了。