<tfoot id='duPLU'></tfoot>

    • <bdo id='duPLU'></bdo><ul id='duPLU'></ul>
    1. <i id='duPLU'><tr id='duPLU'><dt id='duPLU'><q id='duPLU'><span id='duPLU'><b id='duPLU'><form id='duPLU'><ins id='duPLU'></ins><ul id='duPLU'></ul><sub id='duPLU'></sub></form><legend id='duPLU'></legend><bdo id='duPLU'><pre id='duPLU'><center id='duPLU'></center></pre></bdo></b><th id='duPLU'></th></span></q></dt></tr></i><div id='duPLU'><tfoot id='duPLU'></tfoot><dl id='duPLU'><fieldset id='duPLU'></fieldset></dl></div>
      <legend id='duPLU'><style id='duPLU'><dir id='duPLU'><q id='duPLU'></q></dir></style></legend>

      <small id='duPLU'></small><noframes id='duPLU'>

      包名称与文件夹结构不同,但 Java 代码仍可编译

      时间:2023-09-27

        <i id='3ejRo'><tr id='3ejRo'><dt id='3ejRo'><q id='3ejRo'><span id='3ejRo'><b id='3ejRo'><form id='3ejRo'><ins id='3ejRo'></ins><ul id='3ejRo'></ul><sub id='3ejRo'></sub></form><legend id='3ejRo'></legend><bdo id='3ejRo'><pre id='3ejRo'><center id='3ejRo'></center></pre></bdo></b><th id='3ejRo'></th></span></q></dt></tr></i><div id='3ejRo'><tfoot id='3ejRo'></tfoot><dl id='3ejRo'><fieldset id='3ejRo'></fieldset></dl></div>

              <tbody id='3ejRo'></tbody>

            1. <legend id='3ejRo'><style id='3ejRo'><dir id='3ejRo'><q id='3ejRo'></q></dir></style></legend>
              <tfoot id='3ejRo'></tfoot>
              • <bdo id='3ejRo'></bdo><ul id='3ejRo'></ul>

                <small id='3ejRo'></small><noframes id='3ejRo'>

              • 本文介绍了包名称与文件夹结构不同,但 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.

                这篇关于包名称与文件夹结构不同,但 Java 代码仍可编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:为什么包名经常以“com"开头? 下一篇:java包结构反向域名有什么意义

                相关文章

                最新文章

              • <i id='qOC4M'><tr id='qOC4M'><dt id='qOC4M'><q id='qOC4M'><span id='qOC4M'><b id='qOC4M'><form id='qOC4M'><ins id='qOC4M'></ins><ul id='qOC4M'></ul><sub id='qOC4M'></sub></form><legend id='qOC4M'></legend><bdo id='qOC4M'><pre id='qOC4M'><center id='qOC4M'></center></pre></bdo></b><th id='qOC4M'></th></span></q></dt></tr></i><div id='qOC4M'><tfoot id='qOC4M'></tfoot><dl id='qOC4M'><fieldset id='qOC4M'></fieldset></dl></div>
                  <tfoot id='qOC4M'></tfoot>
                  • <bdo id='qOC4M'></bdo><ul id='qOC4M'></ul>

                  1. <small id='qOC4M'></small><noframes id='qOC4M'>

                    <legend id='qOC4M'><style id='qOC4M'><dir id='qOC4M'><q id='qOC4M'></q></dir></style></legend>