简体   繁体   English

错误“找不到主类”

[英]Error “main class not found”

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 . 我所有的Java文件和类文件都在CLASSPATH列出的project文件夹下。

Yet I am getting the following error while running a DriverClass in project folder: 但是,在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.

Can anyone please help me understand the reason here? 有人可以帮我理解这里的原因吗?

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 . 然后使用java project.DriverClass重新执行它。


That said, you should prefer not to use the CLASSPATH environment variable. 就是说,您不应该选择不使用CLASSPATH环境变量。 Yours is currently also invalid. 您的当前也是无效的。 There are spaces in unquoted pathnames. 未加引号的路径名中有空格。 Also, the JDK/bin folder isn't supposed to go in the classpath. 另外,不应将JDK/bin文件夹放在类路径中。 Just use the -cp argument like java -cp . project.DriverClass 只需使用-cp参数(如java -cp . project.DriverClass java -cp . project.DriverClass (while sitting in the package root folder). java -cp . project.DriverClass (位于包根文件夹中)。

Try: 尝试:

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

assuming your package is project 假设你的包裹是project

After compiling if we get a main class not found error, It is simple try this set classpath=%classpath%;.; 编译后,如果出现未找到主类的错误,可以尝试尝试将此set classpath =%classpath%;。;设置为简单。 type this in cmd and run it..problem will be resolved 在cmd中键入并运行它..问题将解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM