Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JavaFX in Netbeans using Maven: some issues taking over a poject

  1. #1
    Junior Member
    Join Date
    Nov 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaFX in Netbeans using Maven: some issues taking over a poject

    I inherited a project that was using Ant in Netbeans and was last worked on over a year ago. Development is on a Mac, with the aim of deploying to Mac, Linux and Windows.

    The last compiled JAR file can run with a recipe like this:

    `%java -Djava.library.path=$HOME/Downloads/javafx-sdk-17.0.9/lib -jar /Users/philip/Downloads/genesis2.jar`

    If I run it with a newer JavaFX SDK, javafx-sdk-20.0.2 also works. However if I use v21, I get a spew of exceptions like

    Exception in thread "JavaFX Application Thread" java.lang.UnsatisfiedLinkError: 'long com.sun.glass.ui.mac.MacAccessible._createGlassAcc essible()'

    I tried to set up a build environment but Netbeans objected to setting it up with Ant, whether I opened the existing project or tried to start from scratch, because the existing Java platform does not support JavaFX. I tried using the OpenJDK but the interface for changing the Java platform was broken.

    So: give up on that and change to using Maven.

    That sort of worked but I could bet get a build that would run with the same recipe:

    `% java -Djava.library.path=$HOME/Downloads/javafx-sdk-21/lib target/Genesis2JavaFX-2.0-SNAPSHOT.jar
    Error: Could not find or load main class target.Genesis2JavaFX-2.0-SNAPSHOT.jar
    Caused by: java.lang.ClassNotFoundException: target.Genesis2JavaFX-2.0-SNAPSHOT.jar`

    I found out how to package all dependencies and that does run, after fixing a few things. For example, whenever there was code like

    X.getClass().getResource(Y);

    I changed it to

    X.getClass().getClassLoader().getResource(Y);

    I am not sure why this worked in the former build but not now.

    Another issue. The Ant build was able to create a JAR file that preserved the subdirectory structure whereas with Maven, I could only find a way to package all resources at the top-level:

    `<resources>
    <resource>
    <directory>src/main/java/org/h3abionet/genesis/view</directory>
    </resource>
    <resource>
    <directory>src/main/java/org/h3abionet/genesis/image</directory>
    </resource>
    <resource>
    <directory>src/main/java/org/h3abionet/genesis/help</directory>
    </resource>
    <resource>
    <directory>src/main/java/org/h3abionet/genesis/css</directory>
    </resource>
    </resources>
    `

    To make all this work, I had to change all references to directory structure to current directory, e.g. change

    view/AdmixtureOptions.fxml

    to

    view/Main.fxml

    etc. throughout the code.

    If I inspect the contents of the JAR file, the earlier build does preserve the directory structure.

    Questions:
    1. Is there a way I can configure Maven in pom.xml to preserve the directory structure when copying resources (non-code files) to JAR?
    2. Is there a way in Netbeans using Maven to create a JAR file that can run on the command line with a pointer to a library as the old Ant build did?
    3. Are there any pointers to best practice in using Maven + Netbean for JavAFX projects?

    PS: some pointers to how to format code or scripts not in Java would be helpful too…

    --- Update ---

    That sort of worked but I could bet get a build that would run with the same recipe:

    Should be

    That sort of worked but I could *not* get a build that would run with the same recipe:

    Seems that this forum dislikes my browsers as some features liked editing posts are broken.

  2. #2
    Junior Member
    Join Date
    Nov 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaFX in Netbeans using Maven: some issues taking over a poject

    A few answers. Here is how to add a path to place resources in the JAR file (in pom.xml):

    <build>
    <resources>
    <resource>
    <directory>src/main/java/org/h3abionet/genesis/view</directory>
    <targetPath>org/h3abionet/genesis/view</targetPath>
    </resource>
    </build>

    To run the command line with a specific JavaFX library this is the basic recipe, assuming you have put the lib directory of a downloaded JavaFX library in /usr/local/lib/:

    JARF=pathtojar/jarfile.jar
    LIB=/usr/local/lib/JavaFX-21/
    MODS=javafx.controls,javafx.fxml
    java --module-path $LIB --add-modules $MODS -jar $JARF

  3. #3
    Junior Member
    Join Date
    Jan 2024
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaFX in Netbeans using Maven: some issues taking over a poject

    To create a JAR file that can be run with a pointer to a library (similar to your old Ant build), you can use the maven-assembly-plugin to create an uber-jar or executable jar.

    Here's an example configuration for creating an executable JAR:
    <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
    <archive>
    <manifest>
    <mainClass>your.main.class.Main</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>

    This is the optimised code for your query as far as I know for further assistance you may visit https://www.programminghomeworkhelp.com/netbeans/ there you will find an expert who can solve your queries.

Similar Threads

  1. Netbeans & Hibernate Issues
    By greg57 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 6th, 2023, 05:18 AM
  2. Maven Compilation Issues in IntelliJ
    By kumud.advani@outlook.com in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2023, 07:22 PM
  3. Replies: 1
    Last Post: June 25th, 2019, 01:50 PM
  4. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM
  5. Maven Issues - mvn install
    By Paolo Futre in forum Java Theory & Questions
    Replies: 5
    Last Post: August 26th, 2009, 05:07 AM