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

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

问题描述

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

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,显示正在使用的子类 -

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

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

另外,使用以下批处理文件,它使用 javacjava:

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

javac -cp bin -sourcepath src -d bin srcMain.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 文件移回 binsubf 文件夹.)如果我把改名后的源码subf文件夹留为SUBF,这样源码就无法编译了,把bin类文件夹重命名为subf> 到 SUBF,然后当我运行批处理文件时,出现编译错误(can't find package 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 binsubf 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,这样源代码就可以编译了,但保留类文件夹名为subfSUBF,然后当我运行批处理文件时,我没有收到任何错误 - 批处理文件编译并运行.这意味着 javac-d 选项不区分大小写,因为它忽略了类 subf 文件夹已重命名为 SUBF,因为在该文件夹中找到的 .class 文件具有最新的日期戳值.

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 确实似乎忽略了大小写的某些事情.javacjava 程序请求一个目录,操作系统通过忽略大小写来搜索它.

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 srcMain.java
srcMain.java:5: package subf does not exist
        subf.Sub.method();

这样做,没有错误:

> javac -cp bin -sourcepath src -d bin srcMain.java srcSUBFSub.java

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

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