NetBeans中Maven构建Java GUI应用可执行JAR失败求助
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
targetfolder—you'll see a new JAR file named something likeFFmpegchunker-1.0.0-jar-with-dependencies.jar(Assembly) orFFmpegchunker-1.0.0-shaded.jar(Shade). - Run it with the command:
(Or use the shaded JAR name if you went with that option.)java -jar target/FFmpegchunker-1.0.0-jar-with-dependencies.jar
Quick Checks to Ensure Success
- Double-check that your
MainScreenclass has a validmainmethod: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

