我运行时无法找到 Java 中的主类
我有一个java swing代码,其中有5个类,主类引用其他四个类,这里我还添加了一些jar,我给出以下cmd进行编译,
C:\Users\FSSD\Desktop\FinalAttempt\install\lib>javac WriteHelper.java JavaDemo.j
ava DataBaseHelper.java FileEncryption.java SendEmail.java -cp dnsns.jar;dsn.jar
;imap.jar;javaws.jar;jce.jar;jsse.jar;jxl-2.6.jar;localedata.jar;mail.jar;mailap
i.jar;pop3.jar;rt.jar;smtp.jar;sqlitejdbc-v056.jar;sunjce_provider.jar;sunmscapi
.jar;sunpkcs11.jar;tools.jar JavaSamp.java
这里JavaSamp持有主类,其他类是WriteHelper,JavaDemo,DataBaseHelper, fileEncription 和 SendEmail 类,当我使用上面的 cmd 进行编译时,它编译成功,当我运行这个类时,我遇到以下异常
C:\Users\FSSD\Desktop\FinalAttempt\install\lib>java WriteHelper.java JavaDemo.ja
va DataBaseHelper.java FileEncryption.java SendEmail.java -cp dnsns.jar;dsn.jar;
imap.jar;javaws.jar;jce.jar;jsse.jar;jxl-2.6.jar;localedata.jar;mail.jar;mailapi
.jar;pop3.jar;rt.jar;smtp.jar;sqlitejdbc-v056.jar;sunjce_provider.jar;sunmscapi.
jar;sunpkcs11.jar;tools.jar JavaSamp
Exception in thread "main" java.lang.NoClassDefFoundError: WriteHelper/java
Caused by: java.lang.ClassNotFoundException: WriteHelper.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: WriteHelper.java. Program will exit.
我该如何克服它,提前感谢...
I have a java swing code In that There are 5 classes, the Main Class refer other four classes, here i added some jar also, i give the following cmd for compiling,
C:\Users\FSSD\Desktop\FinalAttempt\install\lib>javac WriteHelper.java JavaDemo.j
ava DataBaseHelper.java FileEncryption.java SendEmail.java -cp dnsns.jar;dsn.jar
;imap.jar;javaws.jar;jce.jar;jsse.jar;jxl-2.6.jar;localedata.jar;mail.jar;mailap
i.jar;pop3.jar;rt.jar;smtp.jar;sqlitejdbc-v056.jar;sunjce_provider.jar;sunmscapi
.jar;sunpkcs11.jar;tools.jar JavaSamp.java
Here JavaSamp holding mainclass the other classes are WriteHelper,JavaDemo,DataBaseHelper,fileEncription and SendEmail classes, when i Complied using above cmd it Compiled Successfully, When i run this class i having the following Exception
C:\Users\FSSD\Desktop\FinalAttempt\install\lib>java WriteHelper.java JavaDemo.ja
va DataBaseHelper.java FileEncryption.java SendEmail.java -cp dnsns.jar;dsn.jar;
imap.jar;javaws.jar;jce.jar;jsse.jar;jxl-2.6.jar;localedata.jar;mail.jar;mailapi
.jar;pop3.jar;rt.jar;smtp.jar;sqlitejdbc-v056.jar;sunjce_provider.jar;sunmscapi.
jar;sunpkcs11.jar;tools.jar JavaSamp
Exception in thread "main" java.lang.NoClassDefFoundError: WriteHelper/java
Caused by: java.lang.ClassNotFoundException: WriteHelper.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: WriteHelper.java. Program will exit.
How do i overcome it, thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能通过告诉 Java 类源代码来运行该类。你告诉它运行哪个类。例如:
假设
com.foo
包中有一个名为JavaSamp
的类。请注意,编译时,您可能应该使用类似-d .
的内容来告诉它根据源文件中的包名称将类文件放入以当前目录为根的文件夹结构中。You don't run a Java class by telling it about source code. You tell it which class to run. For example:
That's assuming a class called
JavaSamp
in a package ofcom.foo
. Note that when you compile, you should probably use something like-d .
to tell it to put the class files into a folder structure rooted in the current directory, based on the package name within the source file.要运行 java 文件,您需要指定哪个类文件(以
.class
扩展名结尾,但不指定扩展名)以及其余的类和库(带有.jar
扩展名)在类路径中提供。例如
,要编译该文件,请改用
javac
。您所做的就是告诉 java 在WriteHelper
目录(或包)中查找java
。To run a java file, you specify which class file (ends with a
.class
extension but you don't specify the extension) and the rest of the classes and libraries (with a.jar
extension) are provided in the classpath.e.g.
To compile the file, use
javac
instead. What you did is you told java to look forjava
inWriteHelper
directory (or package).您需要将第一步生成的类包含到第二步的类路径中。
You need to include the classes generated in the first step to the class path in the second step.
当您运行 java WriteHelper.java 时,您是在告诉 Java 在“WriteHelper”包中查找名为“java”的类。它不存在,这就是异常告诉您的内容:
将类作为
java WriteHelper
运行。java
命令需要类名...而不是类文件名。编译和运行代码的方式还存在其他问题。
-cp
选项及其值必须出现在 Java 源文件名称(对于javac
)和 Java 类名称(对于java
)。java
命令需要一个类名,而不是很多类名。您需要找出具有public static void main(String[] args)
方法的类,并(仅)使用该类作为java
类参数。 (我猜想,如果您有一个名为Main
的类,它具有main
入口点方法。)只有在所有类都已声明的情况下,这才有效在默认类中。如果源代码以
package
声明开头,则需要将类组织在目录树中,其组件镜像类包;请参阅 @Jon Skeet 的回答。最后,强烈建议您仔细阅读
java
和javac
的手册页,以及讨论类路径如何工作的链接页面。一旦你理解了它们,这些东西就不再像是黑魔法了。When you run
java WriteHelper.java
you are telling Java to look for a class called "java" in the "WriteHelper" package. It isn't there, and that is what the exception is telling you when it says:Run the class as
java WriteHelper
. Thejava
command expects a class name ... not a class file name.There are other problems with the way that you are compiling and running code.
The
-cp
option and its value must appear before the names of the Java source files (forjavac
) and the name of the Java class (forjava
).The
java
command expects ONE class name, not lots of class names. You need to figure out which class is the one with thepublic static void main(String[] args)
method and use that one (only) as thejava
class argument. (I would guess, that if you have a class calledMain
that that has themain
entry point method.)This will only work if the classes are all declared in the default class. If the source code starts with a
package
declaration, you need to organize the classes in a directory tree whose components mirror the class packages; see @Jon Skeet's answer.Finally, you would be well advised to read the manual pages for
java
andjavac
carefully, along with the linked page that talks about how the classpath works. Once you understand them, this stuff won't seem like black magic anymore.