我运行时无法找到 Java 中的主类

发布于 2024-11-16 13:22:59 字数 1629 浏览 4 评论 0原文

我有一个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 技术交流群。

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

发布评论

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

评论(4

╰つ倒转 2024-11-23 13:22:59

您不能通过告诉 Java 类代码来运行该类。你告诉它运行哪个类。例如:

java -cp .;dnsns.jar;...[... as before ...] com.foo.JavaSamp

假设 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:

java -cp .;dnsns.jar;...[... as before ...] com.foo.JavaSamp

That's assuming a class called JavaSamp in a package of com.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.

没企图 2024-11-23 13:22:59

要运行 java 文件,您需要指定哪个类文件(以 .class 扩展名结尾,但不指定扩展名)以及其余的类和库(带有 .jar 扩展名)在类路径中提供。

例如

java WriteHelper -classpath "<directory_where_class_file_exists>;mail.jar/localedata.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.

java WriteHelper -classpath "<directory_where_class_file_exists>;mail.jar/localedata.jar;

To compile the file, use javac instead. What you did is you told java to look for java in WriteHelper directory (or package).

神爱温柔 2024-11-23 13:22:59

您需要将第一步生成的类包含到第二步的类路径中。

You need to include the classes generated in the first step to the class path in the second step.

鸠魁 2024-11-23 13:22:59

当您运行 java WriteHelper.java 时,您是在告诉 Java 在“WriteHelper”包中查找名为“java”的类。它不存在,这就是异常告诉您的内容:

线程“main”java.lang.NoClassDefFoundError中出现异常:WriteHelper/java

将类作为 java WriteHelper 运行。 java 命令需要类名...而不是类文件名。


编译和运行代码的方式还存在其他问题。

  • -cp 选项及其值必须出现在 Java 源文件名称(对于 javac)和 Java 类名称(对于 java)。

  • java 命令需要一个类名,而不是很多类名。您需要找出具有 public static void main(String[] args) 方法的类,并(仅)使用该类作为 java 类参数。 (我猜想,如果您有一个名为 Main 的类,它具有 main 入口点方法。)

  • 只有在所有类都已声明的情况下,这才有效在默认类中。如果源代码以package声明开头,则需要将类组织在目录树中,其组件镜像类包;请参阅 @Jon Skeet 的回答。

最后,强烈建议您仔细阅读 javajavac 的手册页,以及讨论类路径如何工作的链接页面。一旦你理解了它们,这些东西就不再像是黑魔法了。

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:

Exception in thread "main" java.lang.NoClassDefFoundError: WriteHelper/java

Run the class as java WriteHelper. The java 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 (for javac) and the name of the Java class (for java).

  • The java command expects ONE class name, not lots of class names. You need to figure out which class is the one with the public static void main(String[] args) method and use that one (only) as the java class argument. (I would guess, that if you have a class called Main that that has the main 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 and javac 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.

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