简体   繁体   中英

Maven - Executable file from a java project

I need to use maven (for a school project) to create an executable file from a single maven command. I've never used maven and tried many solutions here on stackoverlow. The solutions created a jar file, but the file never opened.

This is my project structure

src
    com
        project
                code
                       swing
                             programm
                                      interface
                                               Main.class

I know this isn't maven convention, however changing it now would mean I would have to adjust the imports (as intelliJ doesn't refactor everything perfectly) for around 40 classes.

<?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>MyGroup</groupId>
<artifactId>myProgramm</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Hello World</name>
<description>Course Project</description>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.25.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit5-engine</artifactId>
        <version>5.0.0-ALPHA</version>
    </dependency>

</dependencies>

<build>

</build>

What do I have to put inside to make an executable file?

You need to add plugin to your pom.xml file

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>com.example.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

and to run the program: mvn clean install exec:java

... here is the link for doc http://www.mojohaus.org/exec-maven-plugin/usage.html

There are possible different solutions, depends on your requirements: https://www.baeldung.com/executable-jar-with-maven

TimurJD's answer is correct however I would like to explain step by step what is actually happening and why.

  • To have a jar be executable the JVM needs to know where your main method is.

  • For that you need a file called META-INF/MANIFEST.MF inside the jar you create.

  • This file must contain a reference to the class containing your main method which is done like this:

      Main-Class: com.example.ClassContainingMainMethod 
  • There are many ways of creating said file but since you are using maven here is the plugin that will help you create this manifest file

     <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.1</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.my.packege.Main</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> 
  • Once you have the plugin in your pom.xml simply run the maven install goal, either from your IDE or the command line. After you should find a folder called target in your project. That folder will contain the executable jar.

  • To run the jar you can call from the command line:

     java -jar MyProject.jar 

It should also be noted that unless you abide by the maven standard of keeping your source code in src/main/java you will have to specify your source folder explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM