0

I am relatively new in Java programming and try to set up a Swing-based GUI with some action buttons (including ActionListener and ActionPerformed sections) as a first project. So far, I managed to create some buttons and assign some functions to them. Applying the standard play button on the main.java, which is then supposed to be my "current file",

Intellij screenshot
the output is working perfectly well:
Swingbutton-panel
The functions are further defined using a spread sheet file.

My problem now is how to use the maven pom.xml file and the Intellij IDE properly in order to create a (fat) jar file, which by doupleclicking will give me the programmed interface shown above. I have tried many things and checked out a lot of websites and youtube tutorials. The most helpful was propably this one so far: https://jenkov.com/tutorials/maven/maven-build-fat-jar.html

Furthermore, I use the Maven "clean package" command trying to build the file. The only things I could acchieve in this respect was to build non-functional jar files or "slim" jars on minimal examples, which just print a "hello world" in the shell when using "java -jre test.jar" command.

Can anyone help? Please pay attention to the "special combination" of Maven, fat-jar-building, the Swing-package and Intellij. Thanks so much in advance, I am excited to take your comments!

<!--<?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.jenkov.myprojectname</groupId>
    <artifactId>my-project-name</artifactId>
    <version>1.1.0</version>
    <packaging>jar</packaging>

    <name>My Project Name</name>
    <!--
        <properties>
            <maven.compiler.source>19</maven.compiler.source>
            <maven.compiler.target>19</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        -->
    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.19.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.19.0</version>
        </dependency>
        <dependency>
            <groupId>org.webjars.npm</groupId>
            <artifactId>rc</artifactId>
            <version>1.2.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.seaglasslookandfeel/seaglasslookandfeel -->
        <dependency>
            <groupId>com.seaglasslookandfeel</groupId>
            <artifactId>seaglasslookandfeel</artifactId>
            <version>0.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.formdev</groupId>
            <artifactId>flatlaf</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>com.formdev</groupId>
            <artifactId>flatlaf-intellij-themes</artifactId>
            <version>3.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-assembly-plugin -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.5.0</version>
        </dependency>


    </dependencies>
    <!-- Thanks for using https://jar-download.com -->

    <!-- https://jenkov.com/tutorials/maven/maven-build-fat-jar.html -->
    <build>
        <finalName>my-project-name</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>

                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

fig01

fig02

fig03

fig04

fig05

Nose1
  • 1
  • 1
  • Can you please add one of your `pom.xml ` experiments that didn't work? The one that looked "most promising" maybe? So we can give on-point hints. The key to your success will be the correct usage of the maven-assembly-plugin. – cyberbrain Mar 19 '23 at 19:08
  • Thanks so much cyberbrain, I am more than happy to do that....see code above. I am pretty sure there is some bs written, sorry for that...background is: with the help of Intellij I find java so nice, elegant and kind of straight-forward to programm, but as concerns a deeper understanding of maven, xml and Intellij configurations apart from the obvious I am kind of lost at the moment. Surely appreciate your help!! – Nose1 Mar 19 '23 at 21:44
  • My Main-Class is currently under src/main/java/ dir, if that is of importance...for path adjustment or something – Nose1 Mar 19 '23 at 21:49
  • After running the `mvn clean pacakge`, there should be a `my-project-name-jar-with-dependencies.jar` in your project target file. Can you find that jar in that folder? – LJ replica Mar 20 '23 at 03:22
  • @LJ replica : Thanks for your question. Yes, I generate two jar files in the target folder named my-project-name.jar and my-project-name-jar-with-dependencies.jar when running the "clean package" command. Both of them just do nothing when double-clicking on them. So there is no error message, but the jars behave like they were empty. The my-project-name.jar has 18kb, the my-project-name-jar-with-dependencies.jar is 0kb. – Nose1 Mar 20 '23 at 09:40
  • @Nose1 The 0kb is odd here as I use your pom.xml and it could generate a non 0kb jar. Try checking if any errors in output when running `mvn clean package`. If this does not help, you may try the IDEA artifact feature to package the flat jar: https://www.jetbrains.com/help/idea/working-with-artifacts.html#configure_artifact – LJ replica Mar 20 '23 at 10:15
  • can you please try install your own version of maven (effectively its a download and unzip) and then use that from a commandline to execute the `mvn clean package` ? Just to be sure from which side the issue comes (maven config vs. IDEA config) – cyberbrain Mar 20 '23 at 12:18
  • Also important: can you please show the edit screen of the "Current File" configuration of IDEA? We don't know of your `Main` class even contains the right `main` method and if it is the class that should be started. And what happens when you start the jar-with-dependencies with `java -jar my-project-name-jar-with-dependencies.jar` from the commandline? Any error messages there? – cyberbrain Mar 20 '23 at 12:20
  • you have 2 builds here intelij and maven, suggest you get it to build with maven separate to intellij, suggest you add the debug output to the maven build see section 6.1.9 here https://books.sonatype.com/mvnref-book/reference/running-sect-options.html , suggest add the archive element to the configuration section of assembly plugin as outlined here https://maven.apache.org/plugins/maven-assembly-plugin/usage.html – Nigel Savage Mar 21 '23 at 15:14
  • Sorry people,
    took me a while to configure everything in the right way, I recognized a mvn-associated problem.
    Now I can answer your questions:
    When I execute the "mvn clean package" command in the Intellij terminal, it gives me this:
    -> see fig01 above
    The jar-files are now of a "more healthy size", but still not at all executable:
    -> see fig02 above
    I am not sure if the following can even work, but just for completeness:
    -> see fig03 above
    – Nose1 Mar 21 '23 at 19:39
  • As concerns die "edit configurations"-configuration of Intellij -> see fig04 above – Nose1 Mar 21 '23 at 19:50
  • @LJ replica: I have followed the instructions given by your link -> see fig05 above. However, the Intellij terminal gives me this: "C:\Program Files\Java\jdk-19\bin\java.exe" -Dfile.encoding=windows-1252 -Dsun.stdout.encoding=windows-1252 -Dsun.stderr.encoding=windows-1252 -jar ......\target\my-project-name.jar Error: Unable to initialize main class Main Caused by: java.lang.NoClassDefFoundError: com/formdev/flatlaf/FlatDarkLaf Process finished with exit code 1 – Nose1 Mar 21 '23 at 20:21
  • @Nose1 Try this https://stackoverflow.com/a/45902851 to fix the `no main manifest attribute` issue and package it again to see if it helps. – LJ replica Mar 22 '23 at 02:50
  • In the maven-jar-plugin you have declared the mainClass as "Main" usually we use a package and then use the fully qualified class name like some.package.Main for the mainClass – Nigel Savage Mar 29 '23 at 17:02

0 Answers0