包名称与文件夹结构不同,但仍可编译 Java 代码 [英] Package name is different than the folder structure but still Java code compiles

查看:29
本文介绍了包名称与文件夹结构不同,但仍可编译 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 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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