I am starting to build a program with SceneBuilder and JetBrains IDE, i want to internationalize the program so i made a properties file. Everything works fine on the SceneBuilder but when i try to run the program with JetBrains i get this exception
/usr/lib/jvm/java-8-oracle/bin/java -Didea.launcher.port=7532 -Didea.launcher.bin.path=/home/paulo/idea-IC-163.12024.16/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/paulo/mysql-connector-java-5.1.41/mysql-connector-java-5.1.41-bin.jar:/home/paulo/ProjetoOrcamentos/WorkManager/out/production/WorkManager:/home/paulo/idea-IC-163.12024.16/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain sample.Main
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:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: No resources specified.
/home/paulo/ProjetoOrcamentos/WorkManager/out/production/WorkManager/sample/sample.fxml:16
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.resolvePrefixedValue(FXMLLoader.java:421)
at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:363)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:325)
at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at sample.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
... 1 more
This is the code i have so far
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setScene(new Scene(root, 1200, 1000));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXML File:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<top>
<HBox spacing="20.0" BorderPane.alignment="CENTER">
<children>
<Button mnemonicParsing="false" text="%newWorkButton">
<cursor>
<Cursor fx:constant="CLOSED_HAND" />
</cursor></Button>
<Button mnemonicParsing="false" text="%dbButton" />
</children>
<padding>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</padding>
<BorderPane.margin>
<Insets />
</BorderPane.margin>
</HBox>
</top>
</BorderPane>
I know that the problem occurred while trying to load the properties file, but why it is working in the SceneBuilder and it doesn't work when i try to run it?
PS: The problem happens when %newWorkButton or %dbButton appears because that is the part where it uses the property file.
I am answering my own post because i was able to find the answer a few days ago.
As you can see in Main i had this line loading the fxml:
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"))
But since i am using a property file i need to load it too, so this is what i did:
ResourceBundle resources = ResourceBundle.getBundle("Language/lang_pt");;
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"),resources);
I'm having a similar issue. I just found this and realized that I had forgotten to load my ResourceBundle and my app is blowing up on an i18n resource. I found my answer on this post:
javaFX application error: No resources specified
you must replace "/" with "." like this
ResourceBundle resources = ResourceBundle.getBundle("Language.lang_pt");
Related
I'll try to keep this short but include as many details as I can, basically i'm trying to create a simple MPG calculator using Javafx but I've ran into a snag, whenever I attempt to run the main file some errors appear regarding the FXMLLoader but I cant for the life of me figure out why, I researched as many Q&A's as I could (and there is a lot of them) and I tried implementing the answers given but none of them worked.
Im pretty sure this is the cause of my pain.
Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));
I am pretty much a newbie when it comes to javafx so any assistance would be appreciated, if more information is required I will provide it.
File Structure
File Structure
Main
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
sample.FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane alignment="center" hgap="10" prefHeight="267.0" prefWidth="251.0" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<AnchorPane prefHeight="279.0" prefWidth="251.0">
<children>
<Label layoutX="12.0" layoutY="14.0" prefHeight="35.0" prefWidth="233.0" text="Miles Per Gallon Calculator" textAlignment="CENTER" underline="true">
<font>
<Font size="19.0" />
</font>
</Label>
<Label layoutX="16.0" layoutY="69.0" prefHeight="27.0" prefWidth="50.0" text="Miles">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="16.0" layoutY="113.0" prefHeight="27.0" prefWidth="68.0" text="Gallons">
<font>
<Font size="18.0" />
</font>
</Label>
<Button layoutX="27.0" layoutY="151.0" mnemonicParsing="false" onAction="#calculateMPG" prefHeight="42.0" prefWidth="206.0" text="Calculate MPG" />
<TextField fx:id="milesField" layoutX="84.0" layoutY="70.0" />
<TextField fx:id="gallonsField" layoutX="84.0" layoutY="114.0" />
<Label layoutX="16.0" layoutY="204.0" prefHeight="27.0" prefWidth="68.0" text="MPG">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField fx:id="mpgField" layoutX="84.0" layoutY="205.0" />
</children>
</AnchorPane>
</children>
</GridPane>
Controller
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import java.text.DecimalFormat;
public class Controller {
DecimalFormat df = new DecimalFormat("#.###"); double mpg;
#FXML
private TextField milesField;
#FXML
private TextField gallonsField;
#FXML
private TextField mpgField;
#FXML
void calculateMPG(ActionEvent event) {
try {
double miles = Double.parseDouble(milesField.getText());
double gallons = Double.parseDouble(gallonsField.getText());
if(gallons == 0){
mpgField.setText("Cannot Divide by zero");
}
else {
mpg = miles / gallons;
mpgField.setText(df.format(mpg));
}
} catch (NumberFormatException e){
mpgField.setText("Please enter real numbers.");
}
}
}
Error
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NoSuchMethodError: 'java.lang.Object sun.reflect.misc.ReflectUtil.newInstance(java.lang.Class)'
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at sample.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
As a quick side note, I was having issues setting up IntelliJ, for some reason when I imported JDK 14 for non-javafx programming it didnt import in any of the Jar files so I had to manually import those modules, same goes for JavaFX modules, here is the images of those as well, if something appears incorrect please let me know.
JDK1
JDK2
JDK3
Edit:
Just did a clean install of IntelliJ, JAvaFX,& JDK14, still got the same issue when I attempted to run the basic JavaFX "hello world" although the errors given are much more descriptive, Ill post the new errors here.
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0x6af6aa0c) 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 #0x6af6aa0c
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at sample.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
Edit 2:
Finally cleaned up the errors, after adding this line (see below) to VM options a single issue appeared. (Yes i put my path)
-p /%EnterPathToJavaFX%/lib --add-modules javafx.controls
Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.base not found
Going to do some tinkering with the modules folder and see if i can fix this.
So after working on this for a couple of hours, i got a handle on the VM options (which was something I wasnt aware of when i first started) but i'm pretty sure the issue was caused by the use of forward and backward slashes, in a lot of the answers I read, they used forwards slashes instead of backwards slashes, I didnt realize this until about ten minutes ago, its a little embarrassing but, lesson learned.
Anyways, if youre still having this issue i'll give a basic rundown on what should be done (although there are a ton of good answers out there, just remember to use backslashes, dont be a dumb dumb like me).
Important Note: This implementation is for Windows Systems
1st:
Ensure that you have your JDK and JavaFX downloaded, try to keep the folder names simple (like JDK14 or JavaFX14).
2nd:
Open a new project and select your "Project SDK", in our case select the JDK you downloaded.
3rd:
After creating the project go to File -> Project Structure, then under 'Project Settings' select Libraries, then click the Plus icon to the right of project settings, select Java then navigate to your JavaFX folder and select 'lib'.
4th:
If you run the project now, you will get a runtime error, instead go to Run (at the top) then select 'Edit Configurations'. In the 'VM Options:' text field enter:
-p *Your_Drive_Partition*:\*path_to_JFX*\JavaFX\lib --add-modules=javafx.controls,javafx.fxml
And now you're done, welcome to JavaFX.
TL;DR: Forward slashes are for URI's and backslashes are for local (windows) files, do not forget this or end up in the same situation as me, going insane from doing the same thing over and over again expecting a different result.
Have a good day.
I'm trying to set up my programing environment in Java.
I Have Ubuntu 18.04LTS / Java SE Runtime Environment (build 13.0.1+9)/ Intellij/ JavaFX Scene Builder 11.0.0
I have followed this tutorial : https://www.youtube.com/watch?v=T3NlWMzPyXM
The following 3 files were created:
Main.java :
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java :
package sample;
import javafx.event.ActionEvent;
public class Controller {
// I start writing the code here:
public void pressButton(ActionEvent event) {
System.out.println("You have pressed the button");
}
}
sample.fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane alignment="center" hgap="10" prefHeight="300.0" prefWidth="300.0" vgap="10" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<Button fx:id="btn_msg" mnemonicParsing="false" onAction="#pressButton" prefHeight="109.0" prefWidth="121.0" text="Button" />
</children>
</GridPane>
When I run application I receive the following output errors:
/usr/lib/jvm/java-13-oracle/bin/java --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED -Djava.library.path=/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib -javaagent:/snap/intellij-idea-community/185/lib/idea_rt.jar=34245:/snap/intellij-idea-community/185/bin -Dfile.encoding=UTF-8 -classpath "/home/leo/IdeaProjects/Java kurs kenis/FX_trial2/out/production/FX_trial2:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/src.zip:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx-swt.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.web.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.base.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.fxml.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.media.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.swing.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.controls.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.graphics.jar" -p /home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.base.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.graphics.jar sample.Main
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0xc9d8652) 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 #0xc9d8652
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at sample.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
I can't find any solution for this. Can anybody help?
Finally I have worked the problem out.
I put the line:
--module-path /home/leo/MyApps/javafx-sdk-13.0.1/lib --add-modules=javafx.controls,javafx.fxml
here:
Run > Edit Configuration > VM options
and:
File > Project structure > Libraries > + choose the directory:
/home/leo/MyApps/javafx-sdk-13.0.1/lib
You used
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Try:
Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
I am very new to java fx , I am just trying to add WebView component. So everytime adding WebView Intellij returns a problem.
Here is output:
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException:
Here is my sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.web.WebView?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="sample.Controller">
<children>
<WebView fx:id="myweb" prefHeight="200.0" prefWidth="200.0" />
</children>
</GridPane>
Here is my controller:
import javafx.fxml.FXML;
import javafx.scene.web.WebView;
public class Controller {
#FXML
private WebView myweb;
}
If I am adding another stuff, everything works well, but after adding WebView it returns error.
Also I heard that WebView is not being supported in jdk 11. Is that true?
Just add the word javafx.web to your VM Options after your modules.
You can use the following line of code in your VM Options and add any other modules you're using.
--add-modules javafx.controls,javafx.fxml,javafx.graphics,javafx.web
I'm hoping someone can either explain to me what I am doing wrong, or point me to the answer if this has been answered previously (I've been rooting around hereabouts for 2 days looking for an answer).
I am trying to create an demo application that launches a JavaFX GUI. When my app is ready to launch the GUI (the app has to take care of things in the background first), I call:
Application.launch(StarFXDemo.class, "");
Here is StarFXDemo.java:
package standalonedemo;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class StarFXDemo extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = null;
try
{
root = FXMLLoader.load(getClass().getResource("StarFXDemoDoc.fxml"));
} catch (IOException ioe)
{
ioe.printStackTrace(System.out);
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
StarFXDemoDoc.fxml is in the JAR file & is the same package as StarFXDemo (rather than in another package or in a sub package). So
getClass().getResource("/standalonedemo/resources/StarFXDemoDoc.fxml")
Comes back with a URL to the file.
StarFXDemoDoc.fxml is this:
<?xml version="1.0" encoding="UTF-8"?>
<?import standalonedemo.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="FX Sim Demo"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="standalonedemo.StarFXDemoDocController">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<VBox prefHeight="85.0" prefWidth="322.0" spacing="5.0" style="-fx-background-color: coral;">
<children>
<Label text="Sim Title" />
<TextField fx:id="SimNameField" onMouseDragExited="#setSimulationName" />
</children>
<padding>
<Insets left="25.0" top="15.0" />
</padding>
</VBox>
<Button fx:id="BuildSphereButton" layoutX="14.0" layoutY="100.0" mnemonicParsing="false" onAction="#buildSphereAction" text="Build Sphere" />
</children></AnchorPane>
</content>
</TitledPane>
And StarFXDemoDocController.java is this:
package standalonedemo;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class StarFXDemoDocController implements Initializable
{
#FXML
private TextField SimNameField;
#FXML
private Button BuildSphereButton;
/**
* RequiredConstructor
*/
public StarFXDemoDocController() {}
#Override
public void initialize(URL url, ResourceBundle rb)
{
}
#FXML
private void buildSphereAction(ActionEvent event)
{
}
#FXML
private void setSimulationName()
{
}
}
There is nothing really in the methods yet because I'm just trying to get the GUI to appear at this time. I have the empty constructor, and the #FXML tags on the GUI elements with fx:id tags (as well as their corresponding methods).
When I run the program, at the point I call
FXMLLoader.load(getClass().getResource("/standalonedemo/resources/StarFXDemoDoc.fxml"));
it throws the following exception (this exception is no longer relevant, see the exception below):
I am assuming I have something formatted wrong, but I have no idea what it is.
Can anyone help me out here?
EDIT/UPDATE:
As per James_D's comments (thank you James, obviously I had been staring at that for way too long...), I fixed the obvious error in my controller and made SimNameField a TextField, rather than a Label (and updated the code block above to reflect this). I also changed the exception handling to use printStackTrace. The changes now get me the following stack trace:
javafx.fxml.LoadException: file:<properPathTo>/StandAloneDemo/dist/StandAloneDemo.jar!/standalonedemo/resources/StarFXDemoDoc.fxml:11
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at standalonedemo.StarFXDemo.start(StarFXDemo.java:35)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: standalonedemo.StarFXDemoDocController starting from SystemClassLoader[55 modules] with possible defining loaders null and declared parents [org.netbeans.MainImpl$BootClassLoader#97e1986, ModuleCL#1fa50985[org.netbeans.api.annotations.common], ModuleCL#7251b0ac[org.openide.awt], ModuleCL#7d1344a8[org.netbeans.api.progress], ModuleCL#26b9eb92[org.netbeans.swing.plaf], ModuleCL#7feb85da[org.openide.dialogs], ModuleCL#15c98cea[org.openide.nodes], ModuleCL#ed11c24[org.openide.windows], ModuleCL#621f6603[org.netbeans.swing.tabcontrol], ModuleCL#2a3ed4eb[org.netbeans.swing.outline], ...40 more]
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:224)
at org.netbeans.ModuleManager$SystemClassLoader.loadClass(ModuleManager.java:722)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 22 more
Caused by: java.lang.ClassNotFoundException: standalonedemo.StarFXDemoDocController
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:222)
... 25 more
So I see the ClassNotFoundException, which got me to start looking at my controller class some more, and I noticed that Scene Builder has all but decided that my TextField doesn't exist as a tagged item (when I assign the fx:id of simNameField to the TextField in Scene Builder, I get the "No injectable field..." warning, and right clicking on the fx:id field for the control only shows the buildSphereButton tag). So I removed the simNameField from the controller class, right clicked on the FXML file in NetBeans, and told it to Make Controller. It added the TextField back with the correct fx:id and #FXML tag. Scene Builder still thinks it's an fx:id without an injectable field.
So I obviously have some manner of disconnect between the FXML and the controller, but I have no clue what it could be?
UPDATE2:
Took out the first stack trace, as it was no longer relevant and updated a few lines of code (like adding the import for the package to the FXML).
Been playing with it. If I remove the controller reference, the GUI launches as expected (so we know the FXML file is loading). If I instantiate a Controller object (before I call FXMLLoader.load(<...>)), it instantiates as expected, and I can verify that it is not null through the NetBeans debugger, so I know that the controller class is compiled, and on the class path.
So why can't the FXMLLoader find it? Should I be using an alternative method to load it? I've thought about just creating the controller object in the code and setting it that way, but that seems to annoy NetBeans when it comes to the fx:controller statement (i.e. If you have elements in the FXML that require a controller, and that statement isn't in there, NetBeans flags it as incorrect).
In the FXML you have
<TextField fx:id="SimNameField" onMouseDragExited="#setSimulationName" />
but in the controller you have
#FXML
private Label SimNameField ;
This tries to assign a text field to a field of type Label, which will cause an error at run time.
FYI for anyone who runs across this, I did solve the problem after a fashion. I still have no idea why the posted setup did not work, I'm thinking it has something to do with paths and package references.
Anyway, I was able to make it work like this:
URL fxmlFile = getClass().getResource("/standalonedemo/resources/StarFXDemoDoc.fxml");
StarFXDemoDocController sdCon = new StarFXDemoDocController();
FXMLLoader loader = new FXMLLoader(fxmlFile);
loader.setController(sdCon);
root = (TitledPane) loader.load();
Now it works as expected.
I created the standard NetBeans JavaFX application and I tried to run it in a browser but I had no luck. I have followed every instruction out there with no luck. I am not doing anything special, standard JavaFX and built it with NetBeans IDE.
Here is the error message I am getting:
CacheEntry[file:/Users/mo/NetBeansProjects/WebAppTest/dist/WebAppTest.jar]: updateAvailable=false,lastModified=Fri Mar 04 07:03:31 EST 2016,length=19089
java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.dir" "read")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.security.AccessController.checkPermission(AccessController.java:884)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at sun.plugin2.applet.FXAppletSecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1294)
at java.lang.System.getProperty(System.java:717)
at java.io.UnixFileSystem.resolve(UnixFileSystem.java:133)
at java.io.File.getAbsolutePath(File.java:556)
at webapptest.WebAppTest.start(WebAppTest.java:24)
at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:134)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.dir" "read")
at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:150)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.dir" "read")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.security.AccessController.checkPermission(AccessController.java:884)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at sun.plugin2.applet.FXAppletSecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1294)
at java.lang.System.getProperty(System.java:717)
at java.io.UnixFileSystem.resolve(UnixFileSystem.java:133)
at java.io.File.getAbsolutePath(File.java:556)
at webapptest.WebAppTest.start(WebAppTest.java:24)
at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:134)
... 4 more
JavaFX application:
FXML File
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="webapptest.FXMLDocumentController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>
Controller:
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
main method
public class WebAppTest extends Application {
#Override
public void start(Stage stage) throws Exception {
File f = new File("FXMLDocument.fxml");
String filePath = f.getAbsolutePath();
Parent root = FXMLLoader.load(getClass().getResource(filePath));
System.out.println(filePath);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Due to the Certificate signing issues: I manually trusted the dist\ directory:
Regarding the FXML File, If I didn't do getAbsolutePath(); and I get the resources using the line code below. I get a different error message:
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
if I didn't use getAbsolutePath() I get the following error:
updateAvailable=true,lastModified=Fri Mar 04 09:12:56 EST 2016,length=19089
java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at webapptest.WebAppTest.start(WebAppTest.java:26)
at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:134)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.NullPointerException: Location is required.
at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:150)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at webapptest.WebAppTest.start(WebAppTest.java:26)
at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:134)
... 4 more
Here is the content of the Jar file:
Since you updated your answer, it's now clear what you actually want to do. Instead of converting a classpath resource (as retrieved with getClass().getResource()) to a file, you should just use the resource directly:
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
You do not want to deal with the File class at all, since the resource is coming from inside of a JAR file. Ditch all of that code.
Also, make sure you have the FXMLDocument.fxml placed inside of the webapptest package. If you have it placed at the root of the classpath, use .getResource("/FXMLDocument.fxml") instead.
To include what has been mentioned in comments: You might need to clean and rebuild your project in order for the resources to show up properly.
The problem is that you are denied permission to the filesystem.
In order for your applet to receive file system permissions, it must either be signed with a code certificate (which costs money) or you need to add a security exception in the Java applet policy file.
Also see:
Allow Java applet to file system access and recording without modifying java policy file?
Default Policy Implementation and Policy File Syntax
Creating a Trusted Applet with Local File System Access Rights
What Applets Can and Cannot Do