java - 执行jar时未找到main

标签 java jar javac

http://pastebin.com/1btVw8Cb

上面的代码中有两个类。

上面是我的代码,当我在 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

/image/ncm1q.jpg

上面的一些照片是我用来展示整个过程的。

所以我认为当我执行 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/

相关文章:

java - 该方法因类型错误而未定义

Java Servlet 没有从 JSP 表单接收值

java - 从可执行 JAR 文件中递归读取图像

grails - 如何将 jar 文件从 jdk 1.4 升级到 jdk 1.6?

Java 的类加载器与 jars-within-jars

java - 复杂搜索查询 JPA

java - 递归问题

Java编译错误无法配置JAVA_HOME

javac : file not found even after settinh HOME_PATH

ant - 如何使javac编译器将输出写入文件和控制台?