0

我一直在尝试编译一个包含 2 个类(可以更多)的项目。这两个类位于我正在处理的同一目录中。我运行以下命令:

C:\Users\Me\Java>javac -d 类 -cp 类 *.java

包文件:cweek.java & nweek.java

package Hello;

public class cweek
{

   public static void main(String[] args)
   {
      nweek.printmeth();
   }
}

nweek.java

 package Hello;

    public class nweek 
    {
        public static void printmeth() 
        {
            System.out.println("I'm here");
        }

    }

现在我去 C:\Users\Me\Java\Classes\Hello 并尝试尝试“java cweek”命令,我收到以下错误:

    Exception in thread "main" java.lang.NoClassDefFoundError: cweek (wrong name: Hello/cweek)

    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
4

2 回答 2

2

您应该使用完全限定的类名从包含您的包结构的目录中运行该类:

> C:\Users\Me\Java\Classes: java Hello.cweek

顺便说一句,您已经交换了命名约定。包名应以小写字母开头,类名应以大写字母开头。所以,应该是——hello.CWeek

于 2013-09-14T22:21:23.223 回答
0

cd 到 cweek.class 文件所在的目录。运行此命令:

java Hello.cweek
于 2013-09-14T22:33:24.717 回答