#How to set up javafx with maven

1 messages ยท Page 1 of 1 (latest)

dim socket
#

I have one m,ain class

package org.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

If I run it...

Error: Could not find or load main class org.example.Main
Caused by: java.lang.ClassNotFoundException: org.example.Main

My pom

<?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>org.example</groupId>
    <artifactId>FalcoCBOT</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>22</maven.compiler.source>
        <maven.compiler.target>22</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javafx.version>19</javafx.version>
        <javafx.maven.plugin.version>0.0.8</javafx.maven.plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>${javafx.maven.plugin.version}</version>
                <configuration>
                    <mainClass>org.example.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
solar egretBOT
#

<@&987246487241105418> please have a look, thanks.

dim socket
#

Tried setting up javafx with processbuilder in maven. Did some stuff and now this main doesn't even start anymore...

rancid thicket
dim socket
#

Already created a new project because I was losing my mind over that shit

#

It works now

rancid thicket
dim socket
#

No it's not working at all, I've remade 5 projects now

rancid thicket
#

show me what you did, and the error

#

and project file structure

dim socket
#
  • My project structure is just standard maven:
    with Project/src/main/java/org.example/Main.java
    Project/pom.xml

  • This is my Pom(see file)

  • Then I went ahead and add the scene builder.
    File>Project Structure>Libraries> press add "+"> select java >

Then select
C:\Users\Falco\AppData\Local\SceneBuilder\runtime\lib\javafx-swt.jar

#

Then I went to rightclick on org.example > new > fxml.file

But there is no FXML

rancid thicket
dim socket
#

It's removed

rancid thicket
#

so what is the error ?

dim socket
#

When I start it now it works, just prints hello world as it should

package org.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}
#

Now I'm just trying to use the scene builder and create a simple GUI.

rancid thicket
#

right

#

so

#

create a second class called App

#

make it extends Application

dim socket
#
package org.example;

public class App extends Application{
}

rancid thicket
#

and in main method, remove the hello world by Application.launch(App.class, args);

rancid thicket
dim socket
#

It's not telling me anything

App.java

package org.example;

public class App extends Application{
}
#

Main.java

package org.example;

public class Main {
    public static void main(String[] args) {
        Application.launch(App.class, args);
    }
}
dim socket
#

It is, application is red

rancid thicket
#

alt+enter on it

#

then import

dim socket
#
import com.sun.jdi.connect.LaunchingConnector;
rancid thicket
dim socket
#

it was the firsto ne

rancid thicket
dim socket
rancid thicket
#

ah

#

it doesn't find javafx

#

right click your pom

#

maven

#

reload project (or whatever its name is, I always forget)

dim socket
#

i pressed load maven changes

rancid thicket
#

an icon appared and you clicked on it right ?

dim socket
#

yeah that icon with the m

#

but I also did the reload on the screenshot

rancid thicket
#

right

#

now try to alt+enter Application again

dim socket
#

Still not finding anything

rancid thicket
dim socket
#

import javafx.application.Application;

rancid thicket
#

yep if found it

dim socket
#

Now its this error for

Application.launch(App.class, args);
#

oh

rancid thicket
#

first fix App

#

import Application here too

dim socket
#

Yeah I got it in both now

rancid thicket
#

right

#

and now in App

#

it should show an error

#

that you can alt+enter again

#

to implement start

dim socket
#

Class 'App' must either be declared abstract or implement abstract method 'start(Stage)' in 'Application

rancid thicket
#

yep

#

alt enter

#

implement start

dim socket
#
public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        
    }
}

okay no errors now

rancid thicket
#

no error in main too ?

dim socket
#

no error,
only warning in app.java

Exception 'java.lang.Exception' is never thrown in the method

rancid thicket
#

right

#

so now, open SceneBuilder

#

and create a scene from a template as an example

dim socket
#

Can't I just open it through intellij? Or I have to launch the app.exe from explorer

#

Because Idk how

rancid thicket
#

wdym you don't know

#

you just run the SceneBuilder you downloaded

dim socket
#

I just want to make sure whatever I create in scenebuidler is updated automatically

dim socket
#

Okay ill just run the exe

rancid thicket
dim socket
#

I now have a basic app and its untitled

rancid thicket
#

right

#

in intellij

#

do you have a resource folder?

dim socket
#

yes

#

resources

rancid thicket
#

then save it under this folder

#

ctrl+s

dim socket
#

Okay I named it gui

#

And it's here now

hot fox
#

and you might have to confiugre it through the preferences, to point the to SB you wanna use

dim socket
#

hmm okay

rancid thicket
dim socket
#

yes I am

ebon arrow
ebon arrow
#

easier to just talk

dim socket
#

I dont mind I just want to set up basic GUI and the rest will follow

hot fox
hot fox
ebon arrow
dim socket
#

So I should not have named it gui.fxml?

ebon arrow
#

to make the setup just start an maven archetype and that's it, most come with a controller example and a view

rancid thicket
#

@dim socket
write
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("gui.fxml"));
then
Parent parent = fxmlLoader.load();
In start

hot fox
ebon arrow
dim socket
#
package org.example;

import javafx.application.Application;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("gui.fxml"));
        Parent parent = fxmlLoader.load();
    }
}
dim socket
#

I think once I get a basic going i'll catch up on the rest

rancid thicket
#

Scene scene = new Scene(parent);
stage.setScene(scene)
stage.show()

#

@dim socket

ebon arrow
solar egretBOT
rancid thicket
#

please stop

ebon arrow
#

that's an example of a flowController class that helps you manage all your views and controllers without much issues

dim socket
#

okay well

package org.example;

import javafx.application.Application;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("gui.fxml"));
        Parent parent = fxmlLoader.load();
        Scene scene = new Scene(parent);
        stage.setScene(scene);
        stage.show();
    }
}

This is my class now

solar egretBOT
rancid thicket
#

I wouldn't advise it

#

and you are just disturbing here

dim socket
#

Ill just get help by A... once I got the basic and I need help ill come to you okay?

rancid thicket
hot fox
#

no need to blacklist em like that

rancid thicket
hot fox
#

there are 3 people here...

dim socket
#

IDK what to import here it just says

#

for FXMLLoader it wants me to make an FXMLLOADER class which i dont thikn is right

rancid thicket
dim socket
#

oh wait

rancid thicket
#

show your pom again please

dim socket
#
import javafx.scene.Parent;
import javafx.scene.Scene;

Added these

#

but still FXMLLoader is red and load

rancid thicket
#

yes

dim socket
rancid thicket
#

show your pom again please

#

ah you do not have fxml

#

add back fxml in your pom

ebon arrow
#

then why not use an archetype?

dim socket
#

How do I add fxml

rancid thicket
rancid thicket
dim socket
#

ok im adding it one sdec

rancid thicket
#

the fxml one

#

do ou understand how dependencies work ?

dim socket
#

<groupId>org.openjfx</groupId>

rancid thicket
#

in maven

dim socket
#

oops

rancid thicket
#
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>${javafx.version}</version>
    </dependency>
dim socket
#

Yeah

solar egretBOT
dim socket
#

I think I got it

rancid thicket
dim socket
#

I'm just working on 1 monitor and both ide and discord have 50% of my screen so it's tought copying

#

okay

#

when I alt enter on FXMLLoader it still asks to create a class

ebon arrow
#

your making him walk trough unnecessary troubleshooting

dim socket
#

Did I add the dependency correct?

rancid thicket
#

did you reload the project ?

dim socket
#

I ctrl+s the pom

#

and reload maven project

#

Did I add it correctly to my pom?

rancid thicket
#

not the whole pom

#

just the fxml part

dim socket
#
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
rancid thicket
#

yes this

#

put it in the dependencies node

#

if it's wrong, it should be red

dim socket
#

I copied that from the POM it was already in there

rancid thicket
#

right

#

now reload project

dim socket
#

still same after reloading and ctrl+s

#

still doing something wrong

#

looks right though?

rancid thicket
#

๐Ÿค”

#

import javafx.fxml.FXMLLoader

dim socket
rancid thicket
#

try to add the import manually

dim socket
#

yeah now it works

rancid thicket
#

ah ?

dim socket
#

no errors at least

#

Should I start it?

rancid thicket
#

yes

#

try it

dim socket
#
Error: JavaFX runtime components are missing, and are required to run this application
ebon arrow
#

dude, just let him use an archetype that comes preconfigured so he actually learns something

dim socket
#

nah I want to fix this now

ebon arrow
#

sure?

dim socket
#

It's under my skin

ebon arrow
#

cuz that's not usually what is made

hot fox
#

just make sure you understand the error

#

thats a common error, lots of documentation on it

dim socket
#

Main.java:

package org.example;

import javafx.application.Application;

public class Main {
    public static void main(String[] args) {
        Application.launch(App.class, args);
    }
}

App.java:

package org.example;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("gui.fxml"));
        Parent parent = fxmlLoader.load();
        Scene scene = new Scene(parent);
        stage.setScene(scene);
        stage.show();
    }
}

solar egretBOT
rancid thicket
#

<mainClass>org.example.Javafx</mainClass>

dim socket
#

It should be Main instead?

rancid thicket
#

well

#

no App

dim socket
#

hmm

rancid thicket
#

since you have the plugin

#

<mainClass>org.example.App</mainClass>

#

should be this

#

and then go at the right tab

#

maven

dim socket
#

Wait do I press the start button from main class or from app class

rancid thicket
#

javafx:run

dim socket
#

Because I get different error messages for both

#

Ok ill try with App

rancid thicket
dim socket
#

App.java

Error: JavaFX runtime components are missing, and are required to run this application
rancid thicket
dim socket
#

Main.java

jun 06, 2024 1:30:38 A.M. com.sun.javafx.application.PlatformImpl startup
WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @c5b7752'
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:1570)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2541)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at org.example.App.start(App.java:14)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more

solar egretBOT
rancid thicket
hot fox
#

do you have a module-info in your project, by any chance?

rancid thicket
#

and it's not the problem

#

there are two ways to run javafx app

dim socket
#

I changed it to app

          <configuration>
                    <mainClass>org.example.App</mainClass>
                </configuration>

But still the same arror

rancid thicket
#

either run it like java app so by running main
or by using the plugin that he has, with javafx:run

dim socket
#

I did save and reload as well

hot fox
#

alathreon, i know how to run javafx apps. it was a question

dim socket
#

Idk what you mean with javafx:run

ebon arrow
rancid thicket
hot fox
#

pulling an ethan here pepekek

rancid thicket
#

anyway

dim socket
#

I'm looking in this menu rn

rancid thicket
#

about the location is not set error

dim socket
#

Can't find it

rancid thicket
#

it's caused because it doesn't find the fxml

rancid thicket
dim socket
solar egretBOT
rancid thicket
rancid thicket
# dim socket

yep same error as running main
you now have two ways to run it pepekek

rancid thicket
dim socket
#

F2 doesn't ren ame files in intellij?

hot fox
#

thats windows

dim socket
#

works in visual code though

ebon arrow
#

Falco, im waiting till you decide to actually use a pre-configured archetype and learn from that

hot fox
#

yup, but not all apps. VS was made by Microsoft

rancid thicket
dim socket
#

Wait so you want me to add a / before the file itsself right?

#

Or am I being dumb rn

rancid thicket
#

no

#

in the path

#

in the fxmlloader

#

"/gui.gxml"

dim socket
#

ahhh

hot fox
#

its shift + f6 for IJ

rancid thicket
#

ah

dim socket
#
package org.example;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/gui.fxml"));
        Parent parent = fxmlLoader.load();
        Scene scene = new Scene(parent);
        stage.setScene(scene);
        stage.show();
    }
}
solar egretBOT
dim socket
#

still same error:Error: JavaFX runtime components are missing, and are required to run this application

rancid thicket
#

yes

#

now try to run it again

dim socket
#

same error :"Error: JavaFX runtime components are missing, and are required to run this application

rancid thicket
#

no

dim socket
#

Pom still has the App

rancid thicket
#

run either main

#

or javafx:run

dim socket
#

ok

rancid thicket
#

not something else

dim socket
#

Its works but still stubborn with error messages

rancid thicket
#

so a few things

hot fox
dim socket
hot fox
#

well, i mean, read em ofc

#

fxml using version 21 loaded by a runtime of 19

#

so still got some shakey ground

dim socket
#

Its probably because I downloaded the software which has newest version

#

and watched some old video which I took the pom off

hot fox
#

the version being used is higher than the runtime being used

#

the code is more recent than the system it runs on

#

not a huge issue, for now at least

dim socket
#

I'm glad

hot fox
#

i mean, its a sign though

#

botched solution

dim socket
#

yeah it feels like that too

rancid thicket
#
  • Note that you did go on a complicated route to create it, maven has something called archetypes, that can automatically create a basic project. So next time, run a javafx archetype ๐Ÿ™‚
  • Note that you do not need fxml, and in fact, you can mix non fxml with fxml as you wish
  • JavaFX terms are inspired from theater : Stage is a window, Scene is what you want to display at a certain moment, a menu is a scene, another menu or whatever is another scene
  • You will have to understand how to create and use controllers with javafx
  • SceneBuilder creates and edit a FXML file, it's based on XML, and you can freely edit it if you wish
    @dim socket
dim socket
#

Cool ๐Ÿ™‚ well thanks I'm suprised you had the patience for it lol

rancid thicket
#

So the next step would be to create a controller

dim socket
#

Controller has some logic for the UI is what I'm assuming

rancid thicket
#

which is basically the part of a scene that is logic

dim socket
#

So whatever happens on the button click

rancid thicket
#

you create a controller class that implement Initilizable

#

then on SceneBuilder, you link the controller class to the scene (tab bottm left)

#

then whenever you want an element in the fxml to be used by the controller, you set it an fx:id in scenebuilder (tab bottom right)

#

and then in the controller, you add a field with tha same name as the fx:id, the corresponding type and the annotation @FXML abose it

#

and javafx will automatically link it so you can access this node

#

and if you want to react to events

#

then in fxml, you select an event you want to listen to and ad the name of the listener

#

and then in the controller, you have a void method of the same name that you put in scenebuilder, corresponding parameters and @FXML above it

#

and javafx will automatically link it

#

and note that all of this can be done without scenebuilder/fxml

#

but from code

#

so you can mix and match fxml with regular javafx as you wish

#

if you have any question, ask, but I'll go to sleep very soon

dim socket
#

So FXML file has all the elements that are in the UI like ubttons, scenes. Which will be automatically added when i press ctrl+s in scene builder

Controller contains the logic and I cann access elements like this e.g:

@FXML
private Button startButton;
    @FXML
    public void startAction() {
        System.out.println("Start button clicked!");
    }

Need to name it to whatever ID it has in scenebuilder same for the event

#

Just making sure I understood

ebon arrow
#

yes

#

there is a plugin called like FXML updater or something like that allows you with a simple right click to automatically load all the ID's and methods attached to your FXML into your controller class

rancid thicket
dim socket
ebon arrow
rancid thicket
dim socket
#

Okay cool I'll try that too

rancid thicket
# ebon arrow

Does it work with setting up the controller too because it's often a mess to set it, scenebuilder never finds it?

ebon arrow
#

it is almost 100% necessary ir more complex scenarios, as you can get goofy bugs if a button doesnt have its ID correctly set or a method is not called

ebon arrow
rancid thicket
#

๐Ÿ˜ฆ

dim socket
#

So what is different from what I have in my project now compared to the archetypre you were talking about

ebon arrow
#

and then the plugin says like: oh, theres a controller here! let's implement what has ID's and methods!

rancid thicket
ebon arrow
hot fox
#

glad to see another fx person around here

rancid thicket
#

it wiill basically generate a project an hellow world with a button in javafx with an fxml already existing and pom correctly made @dim socket

dim socket
#

Oh well, at least I know how to set it up manually now

hot fox
ebon arrow
#

do you want to learn how to create the archetype? its like 5 clicks

#

@dim socket

rancid thicket
#

or a command

#

๐Ÿ™‚

dim socket
rancid thicket
#

note that when using maven, everything can be done via command line

ebon arrow
#

ok, ill send pictures so follow them, okay? its easier

dim socket
#

Sure Ill use a new project

#

Saving this one lol

ebon arrow
#

firstly go to your upper left corner and click on new project

rancid thicket
#

Anyway, good luck ๐Ÿ‘

hot fox
dim socket
ebon arrow
#

then select Maven Archetype, should look like this

#

there you can set up your name, location and JDK version (just go with the default one)

dim socket
#

Yeah okay

ebon arrow
#

thats where you should be

#

there, click on catalog and select maven central

dim socket
#

im using 22 though

#

got it

ebon arrow
#

search for fxml

dim socket
#

why maven central?

ebon arrow
#

or projects

#

select the org.javafx one

rancid thicket
# dim socket why maven central?

maven central is the default and main repository of maven
whenever you write a plugin or dependency in the pom, it will search here by default
I should really stop looking at this chat

dim socket
ebon arrow
#

after selecting it, make shure your javafx version is up to date (just type 21 or 22 there)

ebon arrow
ebon arrow
#

then just create it and run it

dim socket
ebon arrow
#

should work

dim socket
#

I typed 22

#

since its my jkd

#

jdk

ebon arrow
dim socket
#

so just create now?

ebon arrow
#

ye

dim socket
solar egretBOT
dim socket
#

looks good

ebon arrow
#

yep

#

everything ready

#

it comes with a pair of FXML and controllers just for you to learn a bit

dim socket
#

Oh I see

#

It already has the view switch

ebon arrow
#

yep, you can spend a bit there learning how it happens, as switching views is super important

#

if you right click any FXML, intellij should prompt you at the botton of the options to "Open in SceneBuilder"

dim socket
#

yeah

ebon arrow
#

which basically is a "draw it yourself as you like"

#

are u in uni? or just learning by yourself

dim socket
#

I'm already graduated as software engineer lol... But it

#

its my first w eek with java

#

And its pretty different to what I'm use too

ebon arrow
#

javafx can be quite confusing at the start, specially with all that root, node parent child thing

dim socket
#

I rewrote my python app to java, now I just need to build a gui around it. I used forms with netbeans but everyone keeps telling me to use intellij so here i am

#

Yeah, it's new

ebon arrow
#

ngl netbeans can be better in some aspects

#

it already comes with the thing that I told you about a right click load fxml to just load ID's and methods into your controllers and much more related to maven

dim socket
#

Yeah I had something going with it, but at first I just created a UI, then copied the code to a GUI.java class in my project, build all the logic around it. But then I ofcourse couldnt copy it back to netbeans anymore

#

So I couldnt make any changes to it

#

I kind of fked my self

#

So I had to start over, so why not with javafx

#

I should watch documentation before doing something lol

#

anyway why is there app above the fxml files in my project folder?

#

Like why are the classes in top folder and in bottem folder?

ebon arrow
#

think of it this way: when you compile your project, what it really happens is that a ready-to-run program is created inside target and then intellij runs it

#

you can freely ignore it

#

later if you want an executable file like a .jar you can go in there with some plugins and do stuff, but as I said ignore it for now

#

you can even delete it when you want to send your code to someone to free up space

#

@dim socket I advice you to delete the module-info.java, as it can cause some issues if you don't know how to configure it properly; don't worry as java can run without it in most situations

dim socket
#

Ok will do, thanks for the help ๐Ÿ™‚

rancid thicket
dim socket
#

I just created a third controller to see if I understand it and it seems to work fine. The only question I have is about the different panes. I would like to just move around my stuff inside of scene builder without having to retype the margins and such. the example is using vbox which is not allowing me to do that so I want to switch to anchor type. Is there a reason why I should just stay on vbox or is that switch not much of an issue

#

Or what panes would you guys recommend

#

There is a lot to choose from

#

I just want to simply layer some panes on top of each other and just switch them. I don't really care about perfect alignment and such just tryna get a feel for it. So was hoping to do just stuff with my mouse without getting too much into the different panes

#

So far javafx seems to be really easy to use with just linking to event and id ๐Ÿ™‚ I like it so far