【问题标题】:Saving Java program to a runnable format [duplicate]将Java程序保存为可运行格式[重复]
【发布时间】:2021-03-31 10:11:16
【问题描述】:

我已经用 NetBeans 中的 Swing GUI 完成了我的 Java 程序,并且已经构建、清理和运行它没有错误。但是,现在我希望将其保存为可运行的格式,我似乎错过了诀窍。我花了几个小时搜索并尝试了以下所有死胡同或多年来不适用的问题:

  • 在 Eclipse 中打开程序并导出一个可运行的 .jar 文件
  • 在 NetBeans 中运行“构建和清理”以生成“快照”.jar 文件
  • 我尝试从命令行和提升的命令行运行这两个 .jar 文件,但出现不同的错误。

我感觉这与我的 pom.xml 文件有关,但正如我所说,我找不到任何东西,或者我完全没有抓住重点。

这是我的 pom.xml 文件:

<?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.OETrackerV0</groupId>
    <artifactId>OETrackerV0</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
            <type>jar</type>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.formdev</groupId>
            <artifactId>flatlaf</artifactId>
            <version>1.1</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>frameOne</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>
</project>

如有必要,我很乐意编辑和发布其余代码,它也在我的 GitHub 上:https://github.com/ChaseVsGodzilla/oetracker.git

【问题讨论】:

  • “我尝试从命令行和提升的命令行运行这两个 .jar 文件,但出现不同的错误。”你是如何尝试运行它们的,你遇到了什么错误?这些都是需要在问题中提出的非常重要的细节。

标签: java maven netbeans


【解决方案1】:

maven-jar-plugin 会将您的应用程序源打包到目标 JAR 中,但不会打包您的依赖项,因此您最终会得到一个不可执行的存档。

您有两种选择来打包您的应用程序及其依赖项:

Maven 组装插件

您可以使用maven-assembly-plugin assemble:single 目标将您的应用程序打包在一个胖(带有依赖项的jar)中JAR

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>frameOne</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <!-- You need the JAR with dependencies descriptor -->
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <!-- You can 'optionally' bind the plugin to the `package` build phase for automatic execution over builds -->
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Maven 阴影插件

您可以将maven-shade-plugin with custom inclusion and exclusion rule sets 用于您的依赖项/类:

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>frameOne</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <minimizeJar>true</minimizeJar>
                  <filters>
                    <filter>
                       <artifact>org.apache.poi:poi</artifact>
                       <includes>
                           <include>**</include>
                       </includes>
                    </filter>
                    <filter>
                       <artifact>org.apache.poi:poi-ooxml</artifact>
                       <includes>
                           <include>**</include>
                       </includes>
                    </filter>
                    <filter>
                       <artifact>com.formdev:flatlaf</artifact>
                       <includes>
                           <include>**</include>
                       </includes>
                    </filter>
                  </filters>
                </configuration>
              </execution>
            </executions>
        </plugin>
    </plugins>
</build>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 2011-07-13
    • 2019-11-05
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多