简体   繁体   English

通过cmd创建包含库,类和Java文件的可执行jar

[英]Creating an executable jar including libraries and class and java files through cmd

I've tried a few things here and there, I have included all my classes, libraries, MANIFEST.MF, the entire java/bin, along with org.eclipse file that eclipse created, and .classpath, .object file in one folder called testing, and I have attached 2 images of the cmd progress below. 我在这里到那里都尝试了一些东西,我将所有类,库,MANIFEST.MF,整个java / bin以及eclipse创建的org.eclipse文件以及.classpath,.object文件都包含在一个文件夹中称为测试,我在下面附加了2张cmd进度的图像。 The first image, creates an executable jar perfectly but while it is launched, when I press a button to upload a pdf file, it gives the errors in picture two below. 第一张图片完美地创建了一个可执行jar,但是当它启动时,当我按下一个按钮上传pdf文件时,它在下面的第二张图片中给出了错误。

First Image: 第一张图片:

http://buiud.com/creating.png "Creates a jar just fine" http://buiud.com/creating.png “创建一个罐子就好了”

Second Image: 第二张图片:

http://buiud.com/errorafterPdfLoad.png "NullPointerException error" http://buiud.com/errorafterPdfLoad.png “ NullPointerException错误”

Sorry I couldn't add the pictures, due to lack of reputations. 抱歉,由于缺乏声誉,我无法添加图片。

清单文件中的Class-Path条目已损坏,因此JVM找不到包含给出错误的类的jar。

If you are using maven I can recommend you to use the shade plugin to produce runnable jars with all dependencies (and manifest :-)) on-board. 如果您使用的是maven,我建议您使用shade插件在板上生成具有所有依赖项(和manifest :-)的可运行jar。

Include this in your pom.xml : 包括在您的pom.xml中

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <!-- your main class: -->
                  <mainClass>org.sonatype.haven.HavenCli</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

And then execute mvn clean package shade:shade to build a jar that you can run from the command line with java -jar jarfilename.jar . 然后执行mvn clean package shade:shade来构建一个jar,可以使用java -jar jarfilename.jar从命令行运行java -jar jarfilename.jar

At first check your MANIFEST file. 首先,请检查您的清单文件。 You no need to add ".MF" but better use .txt format. 您无需添加“ .MF”,但最好使用.txt格式。

Then ensure that you properly set your MANIFEST.txt look this Warning 然后确保您正确设置了MANIFEST.txt,请看以下警告


Warning: The text file must end with a new line or carriage return. 警告:文本文件必须以换行符或回车符结尾。 The last line will not be parsed properly if it does not end with a new line or carriage return. 如果最后一行未以新行或回车结尾,则将无法正确解析。


http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

So check the fields: Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation) Main-Class: NameOfClassWithMain 因此,检查以下字段:清单版本:1.0创建者:1.7.0_06(Oracle公司)主类:NameOfClassWithMain

So make a new line or carriage return after "Main-Class: NameOfClassWithMain"! 因此,请在“ Main-Class:NameOfClassWithMain”之后添加新行或回车! This field will dissapeared after parsing if you miss that step. 如果您错过该步骤,则在解析后该字段将消失。 Also carefuly check all files in packages. 还要仔细检查软件包中的所有文件。

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

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