0

Would really appreciate help here, please. I´m working on JavaFX application, first time. Tried to build standalone app. I´m not experienced, it´s first time what I see maven or other build tools. Before I build only small applications directly from Eclipse. I understand, that i have to pack other libraries (in maven as dependencies), "compile them", to one .jar file with my source codes. To farthest i got with maven and some .pom files. Tried many combinations of plugins and dependencies, but when i don´t fully understand what i´m doing, i´m always lost in it. Now I´m in state, that when run command in command promt (or build it as it in Eclipse with this goal)

mvn clean javafx:run

project is builded and my App is running. When I (in Eclipse) click on project right click -> Run As -> Maven Install (don´t know which command should I use in command prompt(javafx:install dont works)), It seems like my app compiled to .jar. But when i double click on that .jar, nothing happends. When I run it in command line (java UneccessaryFileCreator-0.0.1-SNAPSHOT.jar) i get error:

Error: Could not find or load main class UneccessaryFileCreator-0.0.1-SNAPSHOT.jar
Caused by: java.lang.ClassNotFoundException: UneccessaryFileCreator-0.0.1-SNAPSHOT.jar

Also I don´t see javafx dependencies in that .jar (when i browsing it in explorer) (but not sure, if i should see something there).

My project is here: https://github.com/JRulik/UneccessaryFileCreator

What is important i guess is structure of project: https://i.stack.imgur.com/aay1Y.png

this .pom file:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>UneccessaryFileCreator</groupId>
  <artifactId>UneccessaryFileCreator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
      <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17.0.1</version>
        </dependency>
    </dependencies>
  
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>

    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>17</release>
        </configuration>
      </plugin>
      
      <plugin>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>0.0.6</version>
        <executions>
            <execution>
                <!-- Default configuration for running -->
                <!-- Usage: mvn clean javafx:run -->
                <id>default-cli</id>
                <configuration>
                    <mainClass>application.Main</mainClass>
                </configuration>
            </execution>
        </executions>
       </plugin>
      
    </plugins>
  </build>
</project>

and maybe mine main class? :

package application;

/**
 * Main Class to init JAVAFX 
 * @author Adminator
 *
 */
public class Main {
    

    /**
     * Main method, using lunch metod on GUIManager.class from JavaFX to run main JavaFX thread
     * (this is made to not need to pass parameters to JVM and due to run .jar only from command line) <-it was not helpful
     * @param args
     */
    public static void main(String[] args) {
        GUIManager.main(args);
    }

}

-> created "fake" Main because i read somewhere, that for this build purpose Main class should not extend some extern library class. So my GUIManager extends Application. Please, would be really thankful for advice, spent last 2 days with this and still haven´t success. I choose maven because it looked like the easiest way. Using standalone plugins in command promt was too dificult for me. Would like to try another apps in JavaFX, but this releasing is really hell (or I´m also not brightest star on the sky)

Javomil
  • 1
  • 2
  • Current [`javafx-maven-plugin`](https://github.com/openjfx/javafx-maven-plugin) version is 0.0.8. How are you creating the JAR's manifest? – trashgod Nov 03 '22 at 22:38
  • Tried with 0.0.8 version without difference. But maybe with JAR´s manifest could be problem. I think i don´t create it (don´t know what it is, never used it before). I thought that with maven only what i need is rightly setup .pom file. – Javomil Nov 03 '22 at 23:18
  • Found Manifest.mf in my .jar file. But not sure if it´s what you mean: ` Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven 3.8.6 Built-By: Adminator Build-Jdk: 17.0.4.1 ` – Javomil Nov 03 '22 at 23:24
  • I use `maven-jar-plugin` to create the manifest `Main-Class` for later launch from the command line; more [here](https://openjfx.io/openjfx-docs/). – trashgod Nov 03 '22 at 23:27
  • tried add that plugin to .pom, nothing changes (still error and same manifest in .jar). I tried official (like you mentioned) guides, also others and some youtube. Like for me somethign is working (building trough maven in eclipse -> application imidietly run). I can build application and run it with VM variables given trough commandline, pass some javafx paths (tried i think with default eclipse build). So something is working. But can´t get working standalone .jar file. And absolutely don´t know, where I´m doing mistake.... – Javomil Nov 03 '22 at 23:48
  • I don't advise using a jar distribution, instead, create a jlink zip using the same javafx-maven-plugin that you are already using (see the documentation for the plugin). If you do that, you don't need *a "fake" Main*, you don't need to *"pack other libraries (in maven as dependencies), "compile them", to one .jar file"*, you also don't need a customized manifest. – jewelsea Nov 04 '22 at 00:12
  • If you really want to create a single jar anyway, then see [Maven Shade JavaFX runtime components are missing](https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing). – jewelsea Nov 04 '22 at 00:14
  • You don't need src in the build output, so you can remove pom sections around `sourceDirectory` and copying resources out of the `src` directory. Source code is not resources and things that are resources should be in the resources directory and the build tools will automatically find and package them by convention. – jewelsea Nov 04 '22 at 00:16
  • I defer to @jewelsea on the greater flexibility of [`jlink`](https://github.com/openjfx/javafx-maven-plugin); editing the manifest is illustrated [here](https://github.com/trashgod/modular/blob/master/pom.xml). – trashgod Nov 04 '22 at 11:29
  • Thanks both of you for help. Still working on it. Tried jlink, now I got some output,but not exactly what i wanted (in \target\ProjectImg\bin I have file with name of the project and .bat, which when I run, my application is running). Wanted more at leaset .exe, if not .jar. Seems lot of messy. Trying now jpack for it, but again lot of struggle (created some .exe, when double click it seems as some msi installer for a sec and then it crash/end). At least it´s 1 file. Think it is some mistake in .pom. Maybe will try that maven shade. If you have some more advice, I will only be happy for them – Javomil Nov 04 '22 at 14:00
  • 1
    There is more info on creating exe files and installers, etc, in the the [packaging info of the JavaFX tag](https://stackoverflow.com/tags/javafx/info). – jewelsea Nov 04 '22 at 18:21

0 Answers0