【问题标题】:executable jar from maven swing project来自 maven swing 项目的可执行 jar
【发布时间】:2017-01-12 08:27:49
【问题描述】:

我是 maven 新手,最近开发了一个 swing 项目,同时使用 maven 管理依赖项。

我现在想做的是打包我的项目以进行分发,并能够将文件传输到另一台计算机/笔记本电脑以供人们打开和使用。

根据我的研究,我了解到我需要使用程序集或 shade maven 插件来打包项目。

我都尝试过,并且已经能够拥有一个可执行的 jar,但我尝试运行它,没有任何反应。

这是我的项目结构HERE

这是我使用 shade 插件时的 POM.xml:

<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>1</groupId>
  <artifactId>Fin</artifactId>
  <version>0.0.6-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>AinsfieldFin</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.14</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.14</version>
    </dependency>

    <dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j</artifactId>
    <version>2.8.0</version>
    </dependency>

    <dependency>
     <groupId>org.apache.xmlgraphics</groupId>
     <artifactId>batik-bridge</artifactId>
     <version>1.7</version>
     </dependency>
  </dependencies>



  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.Fin.index</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <Version>3.1</Version>
                </configuration>
            </plugin>

    </plugins>
  </build>
</project>

这里是文件 index.java

package com.Fin;

//swing imports

import com.Fin.process.ManipulationExcel;
import com.Fin.process.ManipulationWord;


public class index {

    //declarations


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    index window = new index();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public index() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
          // present layout and app functions   
    }
}

我使用 eclipse 作为我的 IDE

提前感谢您提供的任何帮助

已解决:我的 pom.xml 现在看起来像这样:

<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>1</groupId>
  <artifactId>Fin</artifactId>
  <version>0.1.1</version>
  <packaging>jar</packaging>

  <name>AinsfieldFin</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.14</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.14</version>
    </dependency>

    <dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j</artifactId>
    <version>2.8.0</version>
    </dependency>

    <dependency>
     <groupId>org.apache.xmlgraphics</groupId>
     <artifactId>batik-bridge</artifactId>
     <version>1.7</version>
     </dependency>
  </dependencies>



  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.Fin.index</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
                <!-- Build an executable JAR -->
                <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>com.Fin.index</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <Version>3.1</Version>
                </configuration>
            </plugin>

    </plugins>
  </build>
</project>

【问题讨论】:

  • 也许像 this 这样的东西可能会有所帮助

标签: java swing maven executable-jar


【解决方案1】:

您可以通过在 pom.xml 中使用此部分来执行此操作(更改您的主类位置:

 <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <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>com.your.mainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

【讨论】:

  • 你好。谢谢你的帮助,但我不明白你的意思,如果我改变我的主要课程位置,它应该工作吗?我已经替换了你告诉我的部分,并且我得到了另一个 jar,当我运行时,没有任何反应。以防万一我做错了什么,我执行“运行方式”>“Maven 构建...”>然后作为目标“包”
  • 嗨,你把你的主类。像 com.example.Class 这样的位置放到 标记中。您应该执行 mvn clean install 然后转到目标并尝试 java -jar nameOfJar.jar
  • 这有助于加载。这样做并且能够看到我的应用程序发出空指针错误!我纠正了错误并且它现在可以工作了!救生员!
猜你喜欢
  • 2023-04-08
  • 2022-11-07
  • 2014-04-07
  • 2018-07-05
  • 1970-01-01
  • 2019-11-22
  • 2014-11-06
  • 1970-01-01
  • 2021-05-25
相关资源
最近更新 更多