I'm trying to build a small user interface using JavaFX, But I'm getting an error like this:
Error: Could not find or load main class myApp Caused by:
java.lang.NoClassDefFoundError: javafx/application/Application
This is my code:
and im using jdk 12.0.2
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class myApp extends Application{
public static void main(String[] args) {
Phonebook mybook = new Phonebook();
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Group group = new Group();
Scene scene = new Scene(group, 600, 300);
scene.setFill(Color.GRAY);
primaryStage.setTitle("Phone Book");
primaryStage.setScene(scene);
primaryStage.show();
}
This is the Libraries and jdk I'm using:
Image1
I think JavaFX is not a part of the JDK > 9 any more. (Your version is 12.x.x)
Probably this could help you:
https://openjfx.io/openjfx-docs/#install-javafx
(Assuming you are using Maven) If this does not help, try to clean and build your Application.
Sometimes Maven does not recognize newly added dependencies.
Related
I'm using JavaFx on visual studio code IDE. I always get this error:
JavaFX runtime components are missing and are required to run this application
I've already added the VM args and the javafx libraries.
Also some basic FX codes are compiled with ease, but once I use the FXMLLoader class, I get the aforementioned error.
package app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application
{
#Override
public void start(Stage stage)
{
try
{
Parent root = FXMLLoader.load(getClass().getResource("/interfaces/SignIn.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
}
In launch.json, adding a new entry for the vmArgs including the --module-path with the local path to the JavaFX SDK and --add-modules with the required JavaFX modules:
"vmArgs": "--module-path /path/to/javafx/lib --add-modules javafx.controls,javafx.fxml"
This question already has answers here:
JavaFX Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
(12 answers)
What is Difference between ClassNotFoundException vs NoClassDefFoundError vs Could not find or load main class XYZ?
(4 answers)
Closed 1 year ago.
I'm trying to write my first JavaFX program. This is the exact error line:
Error: Could not find or load main class me.GamingCuber.CPSTest.Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
However, I don't know how this error is occuring. Here is the code down below:
package me.GamingCuber.CPSTest;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
Button button;
#Override
public void start(Stage primarystage) throws Exception{
primarystage.setTitle("Test your CPS!");
button = new Button("Click here for CPS");
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
primarystage.setScene(scene);
primarystage.show();
}
}
Try adding arguments:
compile
javac --module-path /path/to/java-fx-libs/ --add-modules javafx.controls,javafx.fxml *.java
run
java --module-path /path/to/java-fx-libs/ --add-modules javafx.controls,javafx.fxml MyMainClass
In eclipse I think you need to navigate to Menu: Window > Preferences > Java > Compiler
I am trying to import javafx (.jar) files into a maven project. I have made a project with Netbeans (Java with Ant -> Java Application) and want to use the same code in Netbeans (Java with Maven -> Java Application). With Ant, I can easily import and use the (.jar) files, but this seems to be a blocked functionality with Maven.
Any solution to this?
I have looked up on several posts and guides online with no luck.
NOTE: Implementing .jar files within maven have been solved. The code does not work though.
Current Error: "Exception running application com.mycompany.ep_game_ref.Game Command execution failed."
package com.mycompany.ep_game_ref;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Game extends Application
{
public static String[] args;
public static Stage primarystage;
public static ObservableList<Items.Item> items = FXCollections.observableArrayList();
public static MainCentral startGame = new MainCentral();
public static Room.Introduction intro = new Room.Introduction();
public static Settings.Difficulty SETTINGS = new Settings.Difficulty();
public static Player player = new Player();
#Override
public void start(Stage stage)
{
this.primarystage = stage;
this.primarystage.setScene(new Scenes.Welcome().setWelcomeScene());
this.primarystage.getIcons().add(new Image("images/aq_logo.png"));
this.primarystage.setResizable(false);
this.primarystage.show();
}
public static void main(String[] args) {
Application.launch();
}
}
You can use JavaFX in a different way:
File>New Project>Maven>JavaFX Application.
I am starting an assignment and just installed javafx from the Eclipse marketplace, after I installed and created a javafx project with fxml however in main the imports are showing errors saying "the import javafx cannot be resolved". I am new to Java and Eclipse and have tried to search for similar questions but they all seem to be different cases.
I would appreciate any help thank you.
Below is the code, and all the imports are showing errors:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Student.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I have figured that it is the library JavaFx SDK which is not able to be added to the project, is anyone good with Eclipse can help solve this problem? I tried configure build path and removing the JavaFx SDK and readding, still does not work.
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
public class LoanCalculator extends Application
{
public void start(Stage myStage)
{
myStage.setTitle("Loan Calculator");
FlowPlane rootNode = new FlowPane();
Scene myScene = new Scene( rootNode, 300, 200 );
myStage.setScene( myScene);
myStage.show();
}
public static void main( String [] args)
{
launch(args);
}
}
This code pops up with multiply errors when I try running it with Eclipse. I apologize if it is an obvious error. I am new to coding, I found this piece of code in my book and I wanted to test it out.
Thanks Guys
Maybe is ir just a typo? In your code it reads: FlowPlane instead of FlowPane.
Java FX is supported by JDK starting from JDK 7 update 6.
Updated my JDK 7 update 5 to JDK 8, executed your code, it worked like a magic.
Also correct the FlowPlane to FlowPane as suggested below by Kornel.