包名称与文件夹结构不同,但 Java 代码仍可编译
我正在使用 Notepad++ 编写我的 Java 代码并使用命令提示符来编译和运行它.以下是我的示例 Java 代码,
I am using Notepad++ to write my Java code and Command Prompt to compile and run it. Following is my sample Java code,
package abraKadabra;
public class SuperClass{
protected int anInstance;
public static void main(String [] abc){
System.out.println("Hello");
}
}
但是,此文件位于以下文件夹结构中:
However, this file is in the following folder structure :
usingprotectedsuperPkg"(usingProtected 是 C 层次结构中某处的文件夹:)
"usingprotectedsuperPkg" (usingProtected is a folder somewhere in the hierarchy in C:)
所以,我在这里的包名应该是 usingProtected.superPkg 而不是我写的 abraKadabra.
So, my package name here should be something like usingProtected.superPkg instead of abraKadabra as I wrote it.
但是,当我从命令提示符编译这个 Java 代码时,它编译得很好,没有错误或警告.为什么会这样?包名不应该遵循文件夹结构吗?如果应该,它将如何坚持?
But, when I compile this Java code from command prompt, it compiles fine with no error or warnings. Why is it so? Shouldn't the package name adhere to the folder structure? And if it should, how would it adhere?
例如如果我的包名是 usingProtected.superPkg,编译器会以相反的顺序检查.当前的工作目录应该是 superPkg,然后父目录应该是 usingProtected 并完成.它是如何检查带有包名的文件夹结构的吗?
For e.g. if my package name is usingProtected.superPkg, will the compiler check in the reverse order. The present working directory should be superPkg, then the parent directory should be usingProtected and its done. Is it how it checks the folder structure with package name?
推荐答案
经过一番试验,我知道了如何使用包名并在命令提示符下运行Java类文件.
After experimenting a bit, I got the way how to use package name and run Java class files from command prompt.
假设以下是我的 Java 源文件:-
Suppose following is my Java source file:-
package mySample;
public abstract class Sample{
public static void main(String... a){
System.out.println("Hello ambiguity");
}
}
此文件位于目录D:Code N CodeCommandLine"中.
This file is in directory "D:Code N CodeCommandLine".
现在,当使用以下命令编译源代码时(通过从 cmd 转到上述目录):-
Now, when compile the source code (by going to the above directory from cmd) using following command:-
javac -d . Sample.java
这会自动在我的当前目录中创建mySample"文件夹.因此,我的类文件 Sample.class 存在于目录D:Code N CodeCommandLinemySample"中.编译器根据我在源代码中提供的包名称创建了这个新文件夹mySample".
This automatically creates "mySample" folder in my current directory. So, my class file Sample.class is present in directory "D:Code N CodeCommandLinemySample". Compiler created this new folder "mySample" from the package name that I gave in my source code.
因此,如果我将包名指定为package com.mySample",编译器将创建两个目录并将我的类文件放在D:Code N CodeCommandLinecommySample"中.
So if I had given my package name to be "package com.mySample", compiler would create two directories and place my class file in "D:Code N CodeCommandLinecommySample".
现在,我仍在当前工作目录中,即在D:Code N CodeCommandLine"中.为了运行我的类文件,我给出了以下命令:
Now, I am still in the present working directory i.e. in "D:Code N CodeCommandLine". And to run my class file, I give the following command:
java mySample.Sample
所以,我给出了包的完整层次结构,然后是类名.Java 解释器将在当前目录中搜索mySample"目录并在该目录中搜索Sample.class".它正确并成功运行它.:)
So, I give the complete hierarchy of package and then the class name. The Java Interpreter will search the current directory for "mySample" directory and in that for "Sample.class". It gets it right and runs it successfully. :)
现在,当我问为什么它编译我的错误包源代码时,它会成功编译代码,但是当我运行我的类文件时它会给出 NoClassDefFoundError.所以上面的方法可以用来从命令行使用包名.
Now, when I asked that why it compiles my wrong package source code, it would compile the code successfully though, but it gives NoClassDefFoundError when I run my class file. So above method can be used to use package names from command line.
相关文章