Home > Software design >  How to make an executable .jar file using maven with local external libraries inside through pom wit
How to make an executable .jar file using maven with local external libraries inside through pom wit

Time:11-22

I'm learning java without an IDE, I want to build a project with FX, and there are several libs, so I want to connect them in a "rough" way. For training, I created a simple program with one method

public void go() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,100);
        frame.add(new JLabel("Test Lib"));
        frame.setVisible(true);
    }

which makes a simple swing-window, calls it Lib, builds it with maven, has testlib.JAR. Then created another program, just

import TEST.Lib;
public static void main( String[] args )
    {
        new Lib().go();
    }

then edited pom, added a plugin to make it executable (it works fine without external libs), added a dependency, and maven created a .jar, but when I run it, it shows no window, when I put code before an external method, it works

<dependency>
      <groupId>testlib</groupId>
      <artifactId>testlib</artifactId>
      <scope>system</scope>
      <version>1.0</version>
      <systemPath>${basedir}\libs\testlib.jar</systemPath>
    </dependency>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>  <!-- for warnings -->
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>libs/</classpathPrefix>
              <mainClass>
                  TEST.App                          
              </mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

In the end, when I run it through cmd, it works fine

javac -cp "path\to\jar\testlib.jar" TEST\App.java
java -cp "path\to\jar\testlib.jar" TEST.App

How to make it work?

CodePudding user response:

What works for me.

Add local dependency

 <dependency>
      <groupId>testlib</groupId>
      <artifactId>testlib</artifactId>
      <scope>system</scope>
      <version>1</version>
      <systemPath>${basedir}/libs/testlib.jar</systemPath>
 </dependency>

In add path to your .jar, and remember version. Then in add these two plugins. First copy dependencies inside target, second make it executable

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>false</overWriteSnapshots>
              <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>TEST.App</mainClass>
            </manifest>
            <manifestEntries>
              <Class-Path>lib/testlib-1.jar</Class-Path>
            </manifestEntries>

          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

In you name a folder in target where will be dependencies. In you should enter main class. In you should enter the path in targer, notice that Maven will add version, in my case testlib.jar => testlib-1.jar

  • Related