1

I am new to IntelliJ and Maven and have, as seems customary by now, encountered a problem. I have made a Maven project within IntelliJ which runs well from within IntelliJ. When I do a clean install however the jar that ends up in the target folder does not run. I get the following error: "Error: Could not find or load main class test2.jar" when trying to run it from the terminal. I expect this has something to do with the MANIFEST file (which I can't seem to find in my project structure) but I really do not know. Is this a common problem or have I been especially careless? Does anyone know of a way to straighten this out?

I do not believe my source code will be especially helpful to you here but I might be wrong. So if you want it I can post it in an edit.

Thanks for any help!

Ivar Eriksson
  • 863
  • 1
  • 15
  • 30
  • What command do you use to run your jar? Is it "java -jar test2.jar"? – y.bedrov May 17 '18 at 14:52
  • @y.bedrov When I run it with that command I an error relating to exactly what I expected: "no main manifest attribute, in test2-1.0-SNAPSHOT.jar". – Ivar Eriksson May 17 '18 at 14:56
  • Possible duplicate of [Can't execute jar- file: "no main manifest attribute"](https://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute) – Dorado May 17 '18 at 15:14
  • That Maven plugin should be able to help you: https://github.com/javafx-maven-plugin/javafx-maven-plugin – Tome May 17 '18 at 15:47

2 Answers2

2

You could create an executable jar which contains all of its dependencies. It sets the class which contains the main() method in the manifest and allows you to run your application:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.example.mainClass</mainClass>
                    </manifest>
                </archive>
                <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>

The fat jar with all of its dependencies (except jre ofcourse) will be built to your target folder. You can run it with:

java -jar your-application-1.0-jar-with-dependencies.jar

To change the output of the jar name or any other tweaks, check out the assembly plugin documentation.

Jaims
  • 1,515
  • 2
  • 17
  • 30
  • Where (in my pom?) do I add this? – Ivar Eriksson May 17 '18 at 16:02
  • @IvarEriksson right inside the root ``project`` tag. It's building configuration, there are lots of other helpful plugins. Don't forget to run ``mvn clean package`` or ``mvn clean install`` (since ``package`` is part of the lifecycle for ``install``) – Jaims May 17 '18 at 16:06
  • Thanks a bunch! You don't happen to know anything about Scene Builder? I can now run this jar but I cannot open it in scene builder even though it is of the correct java version. – Ivar Eriksson May 17 '18 at 16:15
  • @IvarEriksson SceneBuilder is meant to edit fxml files during development. It shouldn't be able to open jars. You edit your fxml files while developing with SceneBuilder, test your application with IntelliJ and finally build and eventually release with Maven. – Jaims May 17 '18 at 16:19
  • Are you sure of this? I'm no expert but as far as I have understood it seems to be the standard way of creating custom components and then reusing them in other projects. See: https://www.youtube.com/watch?v=7is1WTtbZj4 @48:08 – Ivar Eriksson May 17 '18 at 16:26
  • @IvarEriksson If you're building a library with UI components (like in the video) it's a possibility. But in general for using JavaFX to build your business application, you split it up in just FXML files. You'd need to check out how the library is built (perhaps also shown in the video). – Jaims May 17 '18 at 16:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171264/discussion-between-ivar-eriksson-and-jaims). – Ivar Eriksson May 17 '18 at 16:32
1

You have to create an artifact first. Follow these steps

  1. Go to File --> Project Structure
  2. Click on the Artifacts tab
  3. Click the + button and choose JavaFX application [from module 'YourModule'] [enter image description here]1

  4. If you're using any external libraries, you need to include in the artifact like this

    • Right click on (YourApp.jar) --> add copy of --> library files enter image description here
    • Select the library files that are needed for the project and click ok enter image description here
  5. Click on the Java FX tab on the right side, then click the 3 dot button to choose the Application class [The class that extends Application] [enter image description here]4

  6. Click okay, and go to Build --> Build artifacts, and build the project.

DarkMental
  • 482
  • 7
  • 26