简体   繁体   English

jar可执行文件找不到主类-奇怪的一个

[英]Jar executable can't find main class - weird one

Hello I have a strange problem. 你好,我有一个奇怪的问题。 I'm packaging an application with eclipse to produce an executable jar file and when I run it I get the error "Error cant find main class [...]". 我用eclipse打包了一个应用程序以生成可执行的jar文件,当我运行它时,出现错误“无法找到主类错误[...]”。

When I run the same package from the command line example 当我从命令行示例运行相同的程序包时

java -jar app.jar java -jar app.jar

The program launches. 该程序启动。

What is weird is that the error occurs only with applications complied in Java 7 enviroment. 奇怪的是,该错误仅在Java 7环境中编译的应用程序发生。

If I set project to java 6 everything works fine. 如果我将项目设置为Java 6,一切正常。

What should I do to run Java 7 jar executable? 我应该怎么做才能运行Java 7 jar可执行文件?

Thanks for any help. 谢谢你的帮助。

检查您的jre的版本(如果是jre6),则可能是一个问题,尝试将您的jre更新为jre7,这可能会引起麻烦。

You need to set main class in manifest file 您需要在清单文件中设置主类

Sample manifest file 清单文件样本

Manifest-Version: 1.0
Created-By: 1.3.1 (Sun Microsystems, Inc.)
Main-Class: com.package.MainClassName
Class-Path: oc4j.jar

Make sure that you have set all these things like in sample jar file 确保已在示例jar文件中设置了所有这些内容

This issue occurs when the JVM is not able to locate the main class associated with the Jar file, JVM uses Main-Class attribute from Manifest.mf to identify the main class associated 当JVM无法找到与Jar文件关联的主类时,会发生此问题,JVM使用Manifest.mf中的Main-Class属性来标识关联的主类

Seems like you have missed to Add Main-Class Attribute to the Manifest File that was used to create the JAr ,http://www.skylit.com/javamethods/faqs/createjar.html might help 好像您错过了将Main-Class属性添加到用于创建JAr的清单文件中一样,http://www.skylit.com/javamethods/faqs/createjar.html可能会有所帮助

By default a jar is not executable 默认情况下,jar无法执行

Java archives are more often used as libraries where the jar format compresses the code into a single folder. Java归档文件经常被用作库,其中jar格式将代码压缩到一个文件夹中。 Such code is usually not run as an application (standalone), but there is a provision to turn a jar file into an executable application through means of the Manifest file 此类代码通常不作为应用程序运行(独立),但是有一项规定,可以通过清单文件将jar文件转换为可执行应用程序

Simply create a Manifest file indicating which class contains the 'main' method, to make your jar executable and include the Manifest in your .jar 只需创建一个Manifest文件,指示哪个类包含“ main”方法,以使您的jar可执行并将Manifest包含在您的.jar中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM