1

I going to create a runnable JAR file inside Eclipse IDE. All my dependencies are from Maven. In Eclipse I'm creating a JavaFX GUI application for desktop.

The problem is that when I creating the runnable JAR file. I got this error.

enter image description here

The error is very clear.

Jar export finished with problems. See details for additional information. Could not find main method from given launch configuration.

But what is main method? Is it the public static void main(String[] args) method here?

What should I do to solve this?

package se.danielmartensson.start;

import java.util.Optional;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import se.danielmartensson.concurrency.Measureing;

public class Main extends Application{

    /*
     * Start the start(Stage front)
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage front) throws Exception {
        front.setOnCloseRequest(e->{
            e.consume();
            askForClosing();
        });
        Parent root = FXMLLoader.load(getClass().getResource("/se/danielmartensson/fxml/front.fxml"));
        Scene scene = new Scene(root);
        front.setScene(scene);
        front.setTitle("JUBSPlotter");      
        front.show();
    }

    private void askForClosing() {
        Alert question = new Alert(AlertType.CONFIRMATION);
        question.setTitle("Closing");
        question.setHeaderText("Do you want to close?");
        question.setResizable(false);
        question.setContentText("Press OK to close.");
        Optional<ButtonType> answer = question.showAndWait();
        if(answer.get() == ButtonType.OK) {
            Measureing.active = false;
            Platform.exit();
        }

    }

}
José Pereda
  • 44,311
  • 7
  • 104
  • 132
euraad
  • 2,467
  • 5
  • 30
  • 51
  • 1
    Have a look at this question: [Maven Shade JavaFX runtime components are missing](https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing). Simply Eclipse -> Export won't work, and if you are already using Maven, the best option is the shade plugin. Alternatively, if your project is modular, you can use the JavaFX-Maven-plugin and `javafx:jlink` as explained [here](https://openjfx.io/openjfx-docs/#IDE-Eclipse), section Modular with Maven. – José Pereda May 30 '19 at 18:48
  • @JoséPereda Thanks. Now it's to much. I think I will start with your answer :) – euraad May 30 '19 at 19:20
  • The error message is saying the manifest file is missing, the maven-jar-plugin will create this if you use the jar:jar goal. – Martin Spamer May 30 '19 at 20:16
  • @MartinSpamer How? jar:jar goal? A command? Where to input? – euraad May 30 '19 at 20:23
  • @MartinSpamer Ok! I put the "jar:jar" command in the Build Maven window and build it. But still, I cannot create an exe JAR file. – euraad May 30 '19 at 20:33
  • Look in the target folder, is there any jar file? If there is, open it with any tool that will open zip files and see if it contains a manifest.mf file. Make sure that contains a Main-Class entry with the fully qualified name of your Main class. – Martin Spamer May 30 '19 at 21:26
  • @MartinSpamer Yes. I can see a JAR-file now and I can open it :) But to bad it missing Maven dependencies. So the JAR file is full of errors . – euraad May 30 '19 at 21:29
  • @MartinSpamer I think I will use Maven assembly instead of jar:jar – euraad May 30 '19 at 22:03
  • Either maven-assembly-plugin or maven-shade-plugin is the correct way to create an executable jar. – khmarbaise May 30 '19 at 22:21
  • @khmarbaise So what is the correct way? I have seen many different ways to create JAR files with maven. That's the reason in my opinion I think Maven is kind of a mess, because it's no standard way or only-one-way to do. – euraad May 30 '19 at 22:29

1 Answers1

3

You have to use the following maven plugin to create a runnable jar file.

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>
                                com.ddlab.rnd.App
                            </mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

This is for very simple project where you want to learn about how to create an executable jar file to run. To have a good idea about how to create a runnable/executable jar file, check this github link. https://github.com/debjava/runnableJar If you have other libraries to use as part of your application. You have to create fat jar file. To create a fat jar file, you have to use Maven Shade plugin. Find below the link for maven shade plugin.

https://maven.apache.org/plugins/maven-shade-plugin/

Sambit
  • 7,625
  • 7
  • 34
  • 65
  • 1
    Did not work if I add this code-text into the pom.xml file. – euraad May 30 '19 at 19:25
  • Post your pom.xml, Let me check,I will try to add. – Sambit May 30 '19 at 19:26
  • It seams that Maven have stopped working for me. Cannot download any dependencies... – euraad May 30 '19 at 19:26
  • Check your internet connectivity. – Sambit May 30 '19 at 19:27
  • Ofc I have internet ;) – euraad May 30 '19 at 19:28
  • Change the main class, You have added like this com.ddlab.rnd.App. This is from the example, change your class having main method. – Sambit May 30 '19 at 19:31
  • Ok! I have change it now to my main class. IStill not working due to the maven won't download. I don't know happen. It worked yesterday. – euraad May 30 '19 at 19:48
  • Check it in ofc, may be maven is unable to connect, otherwise post the maven error. – Sambit May 30 '19 at 19:49
  • I will check it, then come back and give a reply of the result. – euraad May 30 '19 at 19:51
  • Now Maven is working. I just deleted the .m2 folder. But I still get the same error: "Jar export finished with problems. See details for additional information. Could not find main method from given launch configuration. ". – euraad May 30 '19 at 20:22
  • @DanielMårtensson Have added that maven-jar-plugin, packaged again, i see my main class defined in manifest.mf file in META-INF in packaged .jar, but still get same error: Could not find or load main class. Did you solve this issue? Any tips? – Wortig Mar 26 '21 at 19:15