为什么如果我尝试将其从包装内执行包含main()方法的类我得到一个错误信息? [英] Why if I try to execute a class containing the main() method from within its package I obtain an error message?

查看:314
本文介绍了为什么如果我尝试将其从包装内执行包含main()方法的类我得到一个错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经宣布到一个名为包主要包含的main()方法类的 mainPkg

I have a Main class containing the main() method declared into a package named mainPkg.

现在我使用Ant脚本来用javac进行编译,这tartget:

Now I use an ANT script to perform the compilation with Javac, this tartget:

<target name="compile" depends="clean">

    <mkdir dir="build/classes"/>

    <echo>INTO compile TASK</echo>
    <echo>BASE DIR: ${basedir}</echo>
    <echo>CLASSPATH: ${basedir}\lib\ojdbc6.jar</echo>

    <javac srcdir="src/mainPkg/" destdir="build/classes">

        <classpath>
            <fileset refid="classpath.compile"/>
        </classpath>

    </javac>
</target>

确定它的工作原理,并创建编译的 Main.class 这个目录(位于项目的根)的内部文件:建立/班/ mainPkg / (最后一个目录有包名)

Ok it works and it create the compiled Main.class file inside this directory (situated in the project root): build/classes/mainPkg/ (the last directory have the package name)

好了,现在我的疑问是:为什么如果我去到的建立/班/ 文件夹,在这里我执行:

Ok, now my doubt is: why if I go into build/classes/ folder and here I perform:

java mainPkg.Main

它的工作原理,事实上我获得这个输出(在某些时候有一个例外,但这是什么,我在这个时候提出不相关的另一个问题):

it works, infact I obtain this output (at certain times there is an exception but this is another problem not related at what I am asking at this time):

C:\Projects\edi-sta\build\classes>java mainPkg.Main
Hello World !!!
0
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at mainPkg.Main.main(Unknown Source)

但是,如果我进入建立/班/ mainPkg / 目录(包),它不工作,我获得的无法找到或加载主类错误消息?

But if I enter into the build/classes/mainPkg/ directory (the package) it don't works and I obtain an "impossible to find or load main class" error message?

逸岸:

 Directory di C:\Projects\edi-sta\build\classes\mainPkg

12/02/2015  17:39    <DIR>          .
12/02/2015  17:39    <DIR>          ..
12/02/2015  17:39             1.190 Main.class
               1 File          1.190 byte
               2 Directory   8.091.906.048 byte disponibili

C:\Projects\edi-sta\build\classes\mainPkg>java Main
Errore: impossibile trovare o caricare la classe principale Main

你能解释我为什么会发生?

Can you explain me why it happens?

TNX

推荐答案

如果您在源文件中注意到,有一个为Main.java包语句像这样

If you notice in your source file, there's a package statement for Main.java like this

package mainPkg;
public class Main{

}

当你编译,你说我的类有一个叫做命名空间 mainPkg 。现在,您定义的命名空间为您的类任何试图访问应具备的命名空间preFIX像 mainPkg.Main 即我们通常所说的完全限定类名。

When you're compiling, you're saying that my Main class has a namespace called mainPkg. Now that you defined a namespace for your class any attempt to access Main should have the namespace prefix like mainPkg.Main which we usually refer to as Fully Qualified Class Name.

当你在构建/类和调用的Java mainPkg.Main ,JVM会首先检查是否有一个 mainPkg 子文件夹在当前目录。由于它的存在已经是走了进去,发现了,验证是否完全限定类名称与我们在java命令放弃匹配,如果是相同的,它会加载类并执行的main()。

When you're in build/classes and invoking java mainPkg.Main, JVM will first check if there's a mainPkg sub-folder in the current directory. As it's there already it goes inside and finds the Main, verifies if the fully qualified class name matches with what we gave in java command, if it's same it'll load your class and execute your main().

当你运行在同一个的Java mainPkg.Main 从构建/班/ mainPkg,这一次没有在mainPkg称为mainPkg子文件夹,所以它会抛出你看到的错误。

When you're running the same java mainPkg.Main from build/classes/mainPkg, this time there's no sub-folder called mainPkg within mainPkg, so it will throw the error that you're seeing.

希望这是有道理的:)

这篇关于为什么如果我尝试将其从包装内执行包含main()方法的类我得到一个错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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