上面的代码中有两个类。
上面是我的代码,当我在 Eclipse 中运行时运行良好,在 Netbeans 中也运行良好。
我正在尝试创建一个独立的应用程序,一个 jar 文件。
当我加倍 jar 时出现的错误是:
找不到主类:NewJFrame。程序将会退出。 我从命令提示中得到以下信息:
E:\Java Programs\Eclipse Workspace\test3\src\test3>java testT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: testT/jar
Caused by: java.lang.ClassNotFoundException: testT.jar
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: testT.jar. Program will exit.
我从这里执行了命令:"Could not find the main class: XX. Program will exit." 这就是我在命令 promt 中输入的内容来创建我的 jar:
E:\Java Programs\Eclipse Workspace\test3\src\test3>jar cfm MyJar.jar manifest.tx
t *.class SINGLE.TXT
上面的一些照片是我用来展示整个过程的。
所以我认为当我执行 javac 时问题可能是错误的?但它在 netbeans 和 eclipse 中构建良好并运行良好><请帮忙。
最佳答案
您需要使用-jar
标志。
java -jar MyJar.jar
http://docs.oracle.com/javase/tutorial/deployment/jar/run.html说
JAR Files as Applications
You can run JAR-packaged applications with the Java interpreter. The basic command is:
java -jar jar-file
The
-jar
flag tells the interpreter that the application is packaged in the JAR file format. You can only specify one JAR file, which must contain all the application-specific code.
和http://docs.oracle.com/javase/tutorial/deployment/jar/modman.html解释如何将 list 放入 jar 中。
The
m
option indicates that you want to merge information from an existing file into the manifest file of the JAR file you're creating.
关于java - 执行jar时未找到main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750885/