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

问题描述

我有一个 Main 类,其中包含声明到名为 ma​​inPkg 的包中的 ma​​in() 方法.

现在我用一个ANT脚本来执行Javac的编译,这个tartget:

<mkdir dir="build/classes"/><echo>进入编译任务</echo><echo>BASE DIR: ${basedir}</echo><echo>类路径:${basedir}\lib\ojdbc6.jar</echo><javac srcdir="src/mainPkg/" destdir="build/classes"><fileset refid="classpath.compile"/></classpath></javac></目标>

好的,它会在这个目录(位于项目根目录)中创建编译好的 Main.class 文件:build/classes/mainPkg/(最后一个目录有包名)

好的,现在我的疑问是:为什么如果我进入 build/classes/ 文件夹并在这里执行:

java mainPkg.Main

它有效,事实上我获得了这个输出(在某些时候有一个例外,但这是另一个与我此时问的问题无关的问题):

C:\Projects\edi-sta\build\classes>java mainPkg.Main你好,世界 !!!0java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver在 java.net.URLClassLoader$1.run(Unknown Source)在 java.net.URLClassLoader$1.run(Unknown Source)在 java.security.AccessController.doPrivileged(Native Method)在 java.net.URLClassLoader.findClass(Unknown Source)在 java.lang.ClassLoader.loadClass(来源不明)在 sun.misc.Launcher$AppClassLoader.loadClass(来源不明)在 java.lang.ClassLoader.loadClass(来源不明)在 java.lang.Class.forName0(Native Method)在 java.lang.Class.forName(Unknown Source)在 mainPkg.Main.main(来源不明)

但是如果我进入 build/classes/mainPkg/ 目录(包)它不起作用并且我得到一个不可能找到或加载主类"强>错误信息?

事实:

 目录 di C:\Projects\edi-sta\build\classes\mainPkg12/02/2015 17:39 <DIR>.12/02/2015 17:39 <DIR>..12/02/2015 17:39 1.190 Main.class1 文件 1.190 字节2 目录 8.091.906.048 字节分配C:\Projects\edi-sta\build\classes\mainPkg>java Main错误:不可能的 trovare o caricare la classe principale Main

你能解释一下为什么会这样吗?

Tnx

解决方案

如果你注意到你的源文件中有这样的 Main.java 包声明

package mainPkg;公共类主{}

在编译时,您是在说我的 Main 类有一个名为 mainPkg 的命名空间.现在你为你的类定义了一个命名空间,任何访问 Main 的尝试都应该有类似 mainPkg.Main 的命名空间前缀,我们通常将其称为完全限定的类名.>

当您在构建/类中并调用 java mainPkg.Main 时,JVM 将首先检查当前目录中是否存在 mainPkg 子文件夹.因为它已经存在,它进入并找到 Main,验证完全限定的类名是否与我们在 java 命令中给出的匹配,如果相同,它将加载您的类并执行您的 main().

当您从 build/classes/mainPkg 运行相同的 java mainPkg.Main 时,这次在 mainPkg 中没有名为 mainPkg 的子文件夹,因此它会抛出您正在运行的错误看到了.

希望这是有道理的:)

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

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>

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)

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?

Infact:

 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

解决方案

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

package mainPkg;
public class 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.

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().

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.

Hope this makes sense :)

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

10-10 05:44