为什么javac的-cp选项区分大小写,而java的-cp选项不区分大小写? [英] Why is javac's -cp option case sensitive, but java's -cp option not?

查看:217
本文介绍了为什么javac的-cp选项区分大小写,而java的-cp选项不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 javac java 选项的区分大小写似乎有所不同。例如:

I've noticed that case sensitivity for javac and java options seem to vary. For instance:

区分大小写的 javac 命令选项?

Case sensitive javac command option?

-cp          Yes
-sourcepath  Yes
-d           No

区分大小写的 java 命令选项?

Case sensitive java command option?

-cp          No

作为这四个选项区分大小写的示例,请采用以下文件夹结构:

As an example of case sensitivity for these four options, take the following folder structure:

project \ src \ Main.java
              \ subf \ Sub.java
        \ bin \ Main.class
              \ subf \ Sub.class

Main.java ,显示正在使用的Sub类-

Main.java, which shows the Sub class being used -

class Main {
    public static void main(String[] args) {
        subf.Sub.method();
    }
}

另外,获取以下批处理文件,该文件使用 javac java

Also, take the following batch file, which uses javac and java:

javac -cp bin -sourcepath src -d bin src\Main.java
java -cp bin Main

如果我删除该项目中所有预编译的 .class 文件,并重命名名为 subf SUBF ,然后在运行上面的批处理文件时,出现编译错误,因为它找不到名为的软件包subf 。因此, javac -sourcepath 选项区分大小写。

If I delete all precompiled .class files inside this project, and rename the source code sub folder called subf to SUBF, then when I run the batch file above, I get a compilation error, as it can't find the package called subf. So, javac's -sourcepath option is case sensitive.

(为了使下一个测试成功并运行,需要将现有的 Sub.class 文件移回到 bin中。 \subf 文件夹。)如果我将重命名的源代码 subf 文件夹保留为 SUBF ,这样就无法编译源代码,然后将名为 subf 的bin类文件夹重命名为 SUBF ,然后在我运行批处理文件,但出现错误(找不到软件包subf ),但是可执行文件运行正常。因此, javac -cp 选项区分大小写,但 java -cp 选项不是。 (请注意,按照此SO问题的标题,此观察构成了我的问题的基础。)

(In order for the next test to succeed and run, an existing Sub.class file needs to be moved back in to the bin\subf folder.) If I leave the renamed source code subf folder as SUBF, so that the source code can't be compiled, and rename the bin class folder called subf to SUBF, then when I run the batch file, I get an error (can't find package subf) for compilation, but the executable runs OK. So, javac's -cp option is case sensitive, but java's -cp option isn't. (Please note that as per the title of this SO question, this observation forms the basis of my question.)

最后,如果我将源代码重命名为 SUBF 文件夹返回到 subf ,以便可以编译源代码,但保留名为 subf的类文件夹 SUBF ,那么当我运行批处理文件时,没有任何错误-批处理文件可以编译并运行。这意味着 javac -d 选项不区分大小写,因为它忽略了类 subf 文件夹已重命名为 SUBF ,作为 .class 文件在该文件夹中找到的文件具有最新的datestamp值。

Finally, if I rename the source code SUBF folder back to subf, so that the source code can be compiled, but leave the class folder called subf as SUBF, then when I run the batch file, I don't get any errors - the batch file compiles and also runs. This means that javac's -d option is not case sensitive, as it overlooks the fact that the class subf folder has been renamed to SUBF, as the .class file found inside that folder has an up to date datestamp value.

我已将此简单项目压缩到一个下载中。可以在此处(在DropBox上)找到。

I have zipped up this simple project in to a download. It can be found here (on DropBox).

推荐答案

在匹配程序包和类名时,Java区分大小写。 所有 Java都区分大小写。

Java is case sensitive when it comes to matching package and class names. Everything Java does here is case sensitive.

但是,Windows上的文件系统不区分大小写。它是保留大小写,但不区分大小写。这意味着,对名为 A.class 的文件的请求将发现名为 a.class 的文件(如果存在)。

However, the file system on Windows is not case sensitive. It is case preserving but not case sensitive. This means that a request for a file named A.class would find a file called a.class if it existed.

这说明了Java确实忽略了大小写的原因。 javac java 程序要求一个目录,然后操作系统通过忽略大小写来搜索它。

This explains why some things that Java does appear to be ignoring case. The javac or java program asks for a directory, and the operating system searches for it by ignoring case.

但是您不能依靠它永远是真的。在另一个不能忽略大小写的操作系统上,Java可能找不到在Windows下可以找到的文件。

But you cannot rely on this to always be true. On another operating system that does not ignore case, Java may fail to find files that it would find under Windows.

在那里是您所做的一件事,它会影响命令的结果:

There was one thing you did that affects the outcome of your commands:

您应该将所有Java文件名传递给编译器。如果您使用 ant 进行构建,则将为您完成。

You should pass all the Java file names to the compiler. If you were building with ant, this would have been done for you.

> javac -cp bin -sourcepath src -d bin src\Main.java
src\Main.java:5: package subf does not exist
        subf.Sub.method();

以这种方式执行,没有错误:

Do it this way and there are no errors:

> javac -cp bin -sourcepath src -d bin src\Main.java src\SUBF\Sub.java

这篇关于为什么javac的-cp选项区分大小写,而java的-cp选项不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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