我创建了一个简单的 Java Swing 项目,构建了它,并立即注意到与我看到的其他教程相比有很多差异。 我的目录 我期望找到什么
最重要的是,当我尝试运行它时,生成的 .Jar 文件根本不会发生,即使通过 Java 或将其转换为可执行文件也是如此。这很奇怪,因为当我使用 IDE 本身测试它时它工作正常。
OBS:我正在使用 Apache Netbeans IDE 21。
我尝试将其转换为 .exe 文件,但没有任何效果。
假设您有一个名为
App
的 Swing 应用程序,如下所示:
package org.example;
import java.awt.*;
import java.io.Serial;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.swing.*;
import com.google.common.collect.Lists;
public class App implements Runnable {
private static final String APP_NAME = "My App";
private final Container mainPanel;
public App() {
mainPanel = new MainPanel();
}
@Override
public void run() {
JFrame frame = new JFrame(APP_NAME);
// Set system look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
frame.setContentPane(this.mainPanel);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(new App());
}
private static class MainPanel extends JPanel {
@Serial
private static final long serialVersionUID = 1L;
public MainPanel() {
List<String> words = Lists.newArrayList("Hello", "World");
JLabel label = new JLabel(String.join(" ", words), SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.PLAIN, 64));
setLayout(new BorderLayout());
add(label, BorderLayout.CENTER);
}
}
}
为了使用 Maven 构建它,您需要创建一个具有依赖项的 JAR。
注意:我添加了一个外部库(即 Google Guava)到混合中,这样只有具有依赖项的 JAR 才会执行。
<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>org.example</groupId>
<artifactId>swing-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>swing-maven</name>
<properties>
<main.class>org.example.App</main.class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<!-- Dependencies versions -->
<guava.version>33.2.0-jre</guava.version>
<!-- Plugin versions -->
<maven-assembly-plugin.version>3.7.1</maven-assembly-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<maven-jar-plugin.version>3.4.1</maven-jar-plugin.version>
</properties>
<dependencies>
<!-- Let's include an external library, for the assembly stage -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Use the assembly plugin to build a jar with dependencies -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Use the jar plugin to specify the main class -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- Use the compiler plugin for JDK 21 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>
然后可以使用以下命令运行它:
java -jar target/swing-maven-1.0-SNAPSHOT-jar-with-dependencies.jar