You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetBeans中Maven构建Java GUI应用可执行JAR失败求助

Fixing Executable JAR Issues with NetBeans & Maven (GUI + Third-Party Dependencies)

Hey there! I see you're stuck getting your NetBeans Java app (with GUI and third-party libraries like Jaffree/SLF4J) to run as an executable JAR. The problem right now is that your current maven-jar-plugin only packages your own code—none of those external dependencies are included in the JAR. When you try to run it, Java can't find those missing classes, hence the failure. Let's fix this!

Option 1: Use Maven Assembly Plugin (Simpler for Basic Cases)

This plugin will package your app and all its dependencies into a single "fat JAR" that's ready to run. Here's how to update your pom.xml:

Add this plugin inside the <build><plugins> section (alongside your existing maven-jar-plugin):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
        <descriptorRefs>
            <!-- This tells Maven to include all dependencies -->
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <!-- Point to your main GUI class (matches your existing config) -->
                <mainClass>com.oli.ffmpegchunker.MainScreen</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Option 2: Use Maven Shade Plugin (Better for Dependency Conflict Handling)

If you run into issues with overlapping classes or conflicting dependencies (like your mismatched SLF4J versions: 1.7.5 vs 1.6.4), the Shade Plugin is more robust—it can relocate packages and merge resources. Add this instead of the Assembly Plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.5.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.oli.ffmpegchunker.MainScreen</mainClass>
                    </transformer>
                    <!-- Handle SLF4J resource merging if needed -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                </transformers>
                <!-- Optional: Relocate packages to avoid conflicts -->
                <relocations>
                    <relocation>
                        <pattern>org.slf4j</pattern>
                        <shadedPattern>com.oli.shaded.slf4j</shadedPattern>
                    </relocation>
                </relocations>
            </configuration>
        </execution>
    </executions>
</plugin>

How to Build & Run in NetBeans

  • Save your updated pom.xml.
  • Right-click your project in the NetBeans Projects pane.
  • Select Clean and Build (this ensures old artifacts are removed and the new fat JAR is built).
  • Navigate to your project's target folder—you'll see a new JAR file named something like FFmpegchunker-1.0.0-jar-with-dependencies.jar (Assembly) or FFmpegchunker-1.0.0-shaded.jar (Shade).
  • Run it with the command:
    java -jar target/FFmpegchunker-1.0.0-jar-with-dependencies.jar
    
    (Or use the shaded JAR name if you went with that option.)

Quick Checks to Ensure Success

  • Double-check that your MainScreen class has a valid main method:
    public static void main(String[] args) {
        // Launch your GUI here, e.g.:
        SwingUtilities.invokeLater(() -> new MainScreen().setVisible(true));
    }
    
  • Make sure your SLF4J versions are consistent (right now you have 1.7.5 and 1.6.4—consider updating both to the same recent version like 1.7.36 to avoid potential issues).

内容的提问来源于stack exchange,提问作者Samwinishere Here

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:09:37