java 错误“未找到主类”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3796508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error "main class not found"
提问by name_masked
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 文件和类文件都project
在CLASSPATH
.
Yet I am getting the following error while running a DriverClass
in project
folder:
然而,我DriverClass
在project
文件夹中运行时收到以下错误:
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.accesscd ..
0(Unknown Source)
at java.net.URLClassLoader.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?
任何人都可以帮我理解这里的原因吗?
回答by BalusC
You're inside the package. You should be sitting in the package root. Go one folder up.
你在包裹里面。您应该位于包根目录中。上一个文件夹。
C:\Program Files\Java\jdk1.6.0_21\bin>java project.DriverClass
Then reexecute it using java project.DriverClass
.
然后使用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).
也就是说,您不应该使用CLASSPATH
环境变量。你的目前也无效。不带引号的路径名中有空格。此外,该JDK/bin
文件夹不应该进入类路径。只需使用-cp
像java -cp . project.DriverClass
(坐在包根文件夹中时)这样的参数。
回答by name_masked
Try:
尝试:
##代码##assuming your package is project
假设你的包裹是 project
回答by MAMATHA L
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
编译后如果出现main class not found错误,简单试试这个set classpath=%classpath%;.; 在cmd中输入并运行它..问题将得到解决