错误“未找到主类”

发布于 2024-09-24 15:08:12 字数 1356 浏览 1 评论 0原文

所有,

我的类路径已设置为以下文件夹:

CLASSPATH = .;C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program Files\Java\jdk1.6.0_21\bin\project

我的所有java文件和类文件都在CLASSPATH中列出的project文件夹下。

然而,我在 project 文件夹中运行 DriverClass 时收到以下错误:

C:\Program Files\Java\jdk1.6.0_21\bin\project>java DriverClass
Exception in thread "main" java.lang.NoClassDefFoundError: DriverClass (wrong name: project/DriverClass)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        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$000(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)
Could not find the main class: DriverClass.  Program will exit.

任何人都可以帮我理解这里的原因吗?

All,

My classpath has been set to the following folder:

CLASSPATH = .;C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program Files\Java\jdk1.6.0_21\bin\project

All my java files and class files are under project folder listed in CLASSPATH.

Yet I am getting the following error while running a DriverClass in project folder:

C:\Program Files\Java\jdk1.6.0_21\bin\project>java DriverClass
Exception in thread "main" java.lang.NoClassDefFoundError: DriverClass (wrong name: project/DriverClass)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        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$000(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)
Could not find the main class: DriverClass.  Program will exit.

Can anyone please help me understand the reason here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゞ记忆︶ㄣ 2024-10-01 15:08:12

你在包裹里。您应该位于包根目录中。向上移动一个文件夹。

cd ..

然后使用java project.DriverClass重新执行它。


也就是说,您应该不喜欢使用 CLASSPATH 环境变量。您的目前也无效。未加引号的路径名中有空格。此外,JDK/bin 文件夹不应该位于类路径中。只需使用 -cp 参数,例如 java -cp 。 project.DriverClass (位于包根文件夹中)。

You're inside the package. You should be sitting in the package root. Go one folder up.

cd ..

Then reexecute it using java project.DriverClass.


That said, you should prefer not to use the CLASSPATH environment variable. Yours is currently also invalid. There are spaces in unquoted pathnames. Also, the JDK/bin folder isn't supposed to go in the classpath. Just use the -cp argument like java -cp . project.DriverClass (while sitting in the package root folder).

倾城花音 2024-10-01 15:08:12

尝试:

C:\Program Files\Java\jdk1.6.0_21\bin>java project.DriverClass

假设您的包是 project

Try:

C:\Program Files\Java\jdk1.6.0_21\bin>java project.DriverClass

assuming your package is project

幽梦紫曦~ 2024-10-01 15:08:12

编译后,如果我们收到主类未找到错误,很简单,试试这个
设置类路径=%classpath%;.;
在cmd中输入并运行它..问题将得到解决

After compiling if we get a main class not found error, It is simple try this
set classpath=%classpath%;.;
type this in cmd and run it..problem will be resolved

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文