Reputation: 11
This the 1st problem scenario: When I run this program. which is a javafx UI using .fxml. it runs well when I run it using mvn exec: through maven project configuration. but doesn't run on default .json configuration (pressing f5). however, pressing f5 runs a simple javafx UI without .fxml.
I will post the codes and error for 1st condition below.
the project itself is sound. but here is the main class anyway:
public class MediaAppPlayer extends Application {
@Override
public void start(Stage stage) {
try {
System.out.println("im here ....................");
// Load the FXMmnL file
FXMLLoader loader =
new FXMLLoader(getClass().getResource("/MediaPlayer.fxml"));
Parent root = loader.load();
// Set up the scene
Scene scene = new Scene(root, 500, 200);
// Bind the MediaView size to the scene size
Controller controller = loader.getController();
controller.bindMediaViewSize(scene);
controller.bindHBoxSize(scene);
// Set up the stage
stage.setTitle("Media Player");
stage.setScene(scene);
stage.show();
System.out.println("FXML file loaded sucessfully ....................");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
.json:
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}",
//create java run time component
"vmArgs": "--module-path \"C:/apps/javafx-sdk-21.0.5/lib\" --add-modules javafx.controls,javafx.fxml"
}
]
}
MediaPlayer.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.media.MediaView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.VBox?>
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="demo.Controller">
<center>
<VBox alignment = "CENTER">
<MediaView fx:id="mediaView" />
<Slider fx:id="seekSlider" min="0" max="100" />
</VBox>
</center>
<bottom>
<HBox fx:id="hBox" spacing="10">
<Button text="Open File" onAction="#handleOpenFile" />
<Button text="Play" onAction="#handlePlay" />
<Button text="Pause" onAction="#handlePause" />
<Button text="Stop" onAction="#handleStop" />
<ImageView fitWidth="20" preserveRatio="true">
<image>
<Image url="@icons/volumeicon.png" />
</image>
</ImageView>
<Slider fx:id="volumeSlider" min="0" max="100" />
<Label fx:id="currentTimeLabel" text="00:00" />
<Label fx:id="totalTimeLabel" text="00:00" />
</HBox>
</bottom>
</BorderPane>
Controller Class
public class Controller {
@FXML
private MediaView mediaView;
@FXML
private Slider volumeSlider;
@FXML
private Slider seekSlider;
@FXML
private Label currentTimeLabel;
@FXML
private Label totalTimeLabel;
@FXML
private HBox hBox;
private MediaManager mediaManager;
@FXML
public void initialize() {
hBox.setPadding(new Insets(0,0,10,0));
mediaManager = new MediaManager(mediaView, volumeSlider, seekSlider, currentTimeLabel, totalTimeLabel);
}
public void bindMediaViewSize(Scene scene) {
// Bind the MediaView size to the scene size
mediaView.fitWidthProperty().bind(scene.widthProperty());
mediaView.fitHeightProperty().bind(scene.heightProperty().subtract(60)); // Adjust height to account for controls
// Preserve the aspect ratio of the media
mediaView.setPreserveRatio(true);
}
// create a method to Bind the HBox and seeker slider size to the scene size
public void bindHBoxSize(Scene scene) {
// Bind the HBox and seeker slider size to the scene size
hBox.prefWidthProperty().bind(scene.widthProperty());
seekSlider.prefWidthProperty().bind(scene.widthProperty());
}
@FXML
private void handlePlay() {
mediaManager.play();
}
@FXML
private void handlePause() {
mediaManager.pause();
}
@FXML
private void handleStop() {
mediaManager.stop();
}
@FXML
private void handleOpenFile() {
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(null);
if (file != null) {
mediaManager.loadMedia(file);
}
}
}
pom.xml
<?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>demo</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>21</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Compiler Plugin. here source and target has same java version to fix compatibility while build the project -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<!-- Maven Exec Plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>demo.MediaAppPlayer</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Error
C:\Users\Kiran\OneDrive\Documents\VS Code\Media_Player\demo> cmd /C ""C:\Program Files\Java\jdk-21\bin\java.exe" -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:55228
@C:\Users\Kiran\AppData\Local\Temp\cp_4l8owl80fisy30v5w8p9jmqto.argfile demo.MediaAppPlayer "
im here ....................
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1135)
Caused by: java.lang.RuntimeException: Exception in Application start method
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x3a6815b) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x3a6815b
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2143)
at demo.MediaAppPlayer.start(MediaAppPlayer.java:19)
at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
... 1 more
Exception running application demo.MediaAppPlayer
note: that my project runs through maven pom command line instructions i.e mvn -exec:
but it never seems to run through the default .json configurations. with pressing f5. and I have managed to run javafx programs without fxml that way.
1st Scenerio: things I have done in this project, java fx runs without fxml: -added dependencies using maven. -checked java fx, jdk versions compatibility (they are good). -added right vm args and main class in .json file
2nd Scenerio: without project framework and while creating a javafx program without fxml: -I created lib,src,bin folders. -added .jar files in lib. -added right vm arguments pointing to that library. -(optional) added classpaths in .json file. ( iam not sure how right it was)
Both of this condition doesn't work. idk what I am doing wrong. what I want to know is, how can I run a javafx project on VScode manually. which I tried and didn't work in the 2ndcondition. I can run it using a project manager. and in 1st condition, I used maven project manager to run a javafx program with .fxml file. it did work with mvn exec commands. but not with .json configuration. mind you, it runs if it is just javafx app without using .fxml file. what am i doing wrong here?
Upvotes: 0
Views: 65