Maven - Executable file from a java project
Asked Answered
I

2

5

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?

Irrelevant answered 7/2, 2019 at 13:48 Comment(4)
In executable you mean a platform-specific binary or a jar file, which can be executed using JVM installed on the PC?Arnaud
"as intelliJ doesn't refactor everything perfectly" why do you say that? I've not had a problem with that...basically ever, with any IDE I've used.Caraway
By maven default convention java sources should be placed in src/main/javaTenement
@Arnaud by executable I mean a jar file, should have clarified that. At: VLAZ I also usually never had a problem with refactoring, however, this time it didn't work. Don't know why...Irrelevant
P
2

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

Perla answered 7/2, 2019 at 13:52 Comment(3)
I'm getting a java.lang.ClassNotFoundException. Trying to execute the file with norbert's idea, also gives me the error message that the main class couldn't be foundIrrelevant
I've seen to have found the solution. I added the <sourceDirectory></sourceDirectory> to my build. Now I'm getting new errors that do not seem related to this one.Irrelevant
@Irrelevant You can share the error and maybe some one will help with itPerla
H
4

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.

Humbertohumble answered 7/2, 2019 at 14:30 Comment(0)
P
2

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

Perla answered 7/2, 2019 at 13:52 Comment(3)
I'm getting a java.lang.ClassNotFoundException. Trying to execute the file with norbert's idea, also gives me the error message that the main class couldn't be foundIrrelevant
I've seen to have found the solution. I added the <sourceDirectory></sourceDirectory> to my build. Now I'm getting new errors that do not seem related to this one.Irrelevant
@Irrelevant You can share the error and maybe some one will help with itPerla

© 2022 - 2024 — McMap. All rights reserved.