简体   繁体   English

将Jar文件作为独立应用程序执行

[英]Execute Jar file as standalone application

I want to execute jar file as standalone application. 我想将jar文件作为独立应用程序执行。 When I run the below command I get this error message: 当我运行以下命令时,我收到此错误消息:

[rcbandit@Laptop target]$ /opt/jdk1.8.0/bin/java -jar DX57DC-1.0.jar
no main manifest attribute, in DX57DC-1.0.jar
[rcbandit@Laptop target]$ 

This is the POM configuration: 这是POM配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dx57dc</groupId>
    <artifactId>DX57DC</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>DX57DC</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mainClass></mainClass>
    </properties>

    <organization>
        <name>Corporation Name</name>
    </organization>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeScope>system</excludeScope>
                            <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <mainClass>com.dx57dc.main.DX57DC</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I suppose that I'm missing a maven plugin. 我想我缺少一个Maven插件。 Can you tell me how I can fix this? 你能告诉我如何解决这个问题吗?

Your jar doesn't contain a Manifest that specifies the executable main() function, so java doesn't know which class to execute/start. 您的jar不包含指定可执行main()函数的清单,因此java不知道要执行/启动哪个类。

When using maven, have a look at the assembly plugin. 使用Maven时,请查看Assembly插件。 This enables you to create a jar with the correct Manifest. 这使您可以创建带有正确清单的罐子。

Or simply start your programm with: java -cp DX57DC-1.0.jar 'your_main_class_here' 或者直接使用以下命令启动程序: java -cp DX57DC-1.0.jar 'your_main_class_here'

Regards, 问候,

Mike 麦克风

Create your jar as follows: 如下创建您的jar:

  • Open Command Prompt or Terminal and locate the directory where all Classes are stored 打开命令提示符或终端,然后找到所有类的存储目录
  • Type following command: 输入以下命令:

    jar cfev YourJarName.jar EntryClass *

Or, If Your classes are in some package then, 或者,如果您的课程在某些软件包中,

  • Go Outside Your Package Folder and run the following command: 进入“程序包文件夹”外部,然后运行以下命令:

    jar cfev YourJarName.jar YourPackage.EntryClass YourPackage/*

This will create a jar file. 这将创建一个jar文件。 Now if Double Clicking does not open the jar then, 现在,如果双击没有打开罐子,

  • Locate the Directory in the Terminal or Cmd, where the jar file is kept, and run the following command: 在终端或Cmd中找到保存jar文件的目录,然后运行以下命令:

    java -jar YourJarName.jar args

Here Options were: 这里的选项是:

    -c  create new archive
    -f  specify archive file name
    -e  specify application entry point for stand-alone application
        bundled into an executable jar file
    -v  generate verbose output on standard output

Hope this will Help. 希望这会有所帮助。

try adding something like this to plugins: 尝试在插件中添加如下内容:

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>attached</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.package.to.my.Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>

In order for a JAR file to be executable it needs to have a manifest file with Main-Class and Class-Path entries: 为了使JAR文件可执行,它需要具有包含Main-ClassClass-Path条目的清单文件:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>my.package.App</mainClass>               
                    </manifest>           
                </archive>
                <executions>
                    <execution>
                        <id>default-package</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </configuration>
        </plugin>

This produces a MANIFEST.MF file under META-INF directory in the root of your jar file. 这将在jar文件的根目录下的META-INF目录下生成MANIFEST.MF文件。 Only relevant entries listed: 仅列出相关条目:

Class-Path: lib/somejar.jar
Main-Class: my.package.App

The Class-Path states that in the directory where the jar file resides there exists a lib folder with file somejar.jar in it. Class-Path指出jar文件所在的目录中存在一个lib文件夹,其中包含somejar.jar文件。

The Main-Class states that the file App.class exists in package my.package and its main method will be run. Main-Class声明文件App.class存在my.package文件,它将运行其main方法。

update 更新

If the lib folder isn't present the execution will fail when first dependent class is to be loaded. 如果lib文件夹不存在,则在加载第一个从属类时执行将失败。 To evade this you can pack all the dependencies in your jar with the shade plugin: 为了避免这种情况,您可以使用shade插件将所有依赖项打包到jar中

       <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">
                                <mainClass>my.package.App</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Since all the dependencies are packed together the Class-Path entry is not necessary any more. 由于所有依赖项都打包在一起,因此不再需要Class-Path条目。

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

相关问题 在Grails Web应用程序中使用独立的JAR文件 - Use a standalone JAR file in a Grails web application 与Web应用程序同时运行jar文件(独立的Java控制台应用程序) - Runing a jar file (standalone java console application) simultaneously with a Web Application 独立 jar 和使用它的服务之间的 java 应用程序属性文件 - java application properties file in between standalone jar and services which use it 如何在Java应用程序中使用jython独立jar文件 - How to use jython standalone jar file in a Java application 如何在Node JS应用程序中执行jar文件 - How to execute a jar file in node js application Springboot独立JAR应用程序的JNDI配置 - JNDI configuration for Springboot standalone JAR application Java Standalone可运行GUI应用程序-无法获取jar文件来打开程序 - Java Standalone runnable GUI application - can't get jar file to open the program Log4j Logger在Java独立应用程序中。 jar文件中的属性路径 - Log4j Logger in Java standalone application. properties path in jar file 是否可以提取 JDK 系统模块并将其用作独立的应用程序模块/JAR 文件? - Is it possible to extract JDK system module and use it as a standalone application module/JAR file? 无法执行.jar文件 - Unable to execute .jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM