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

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

usingprotected\superPkg(usingProtected是C中层次结构中的某个文件夹):

"usingprotected\superPkg" (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,那么父目录应该是使用保护并完成它。它是如何用包名检查文件夹结构的?

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 Code\CommandLine。

This file is in directory "D:\Code N Code\CommandLine".

现在,使用以下命令编译源代码(通过从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 Code \CommandLine \ mySample中。编译器从我在源代码中给出的包名创建了这个新文件夹mySample。

This automatically creates "mySample" folder in my current directory. So, my class file Sample.class is present in directory "D:\Code N Code\CommandLine\mySample". Compiler created this new folder "mySample" from the package name that I gave in my source code.

所以如果我将包名称命名为package com.mySample ,编译器会创建两个目录,并将我的类文件放在D:\Code N Code \CommandLine \ com \ mySample中。

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 Code\CommandLine\com\mySample".

现在,我我仍然在目前的工作目录中,即在D:\ Code N Code\CommandLine中。为了运行我的类文件,我给出以下命令:

Now, I am still in the present working directory i.e. in "D:\Code N Code\CommandLine". 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天全站免登陆