Reputation: 1223
I'm trying to run my first JavaFX example, and I'm stumped because I cannot get the following example to run. I have read related items about java.lang.NoClassDefFoundError and I'm guessing it's a CLASSPATH issue, but I'm still new enough to java that I could not figure out what the problem might be from the other similar question here and elsewhere on the Internet. The same error occurs whether CLASSPATH is NOT set, or set to the value listed at the end. Any pointers would be much appreciated.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
It compiles, but when I run it I get the following stack trace:
> javac HelloWorld.java
> java HelloWorld
Exception in Application start method
java.lang.NoClassDefFoundError: Could not initialize class com.sun.javafx.css.StyleHelper
at com.sun.javafx.css.StyleManager$StylesheetContainer.getStyleHelper(StyleManager.java:1390)
at com.sun.javafx.css.StyleManager$StylesheetContainer.access$1300(StyleManager.java:1039)
at com.sun.javafx.css.StyleManager.getStyleHelper(StyleManager.java:976)
at javafx.scene.Node.impl_createStyleHelper(Node.java:7433)
at javafx.scene.Node.impl_processCSS(Node.java:7403)
at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
at javafx.scene.Node.processCSS(Node.java:7383)
at javafx.scene.Scene.doCSSPass(Scene.java:446)
at javafx.scene.Scene.access$3800(Scene.java:170)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2202)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:363)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:460)
at com.sun.javafx.tk.quantum.QuantumToolkit$9.run(QuantumToolkit.java:329)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Thread.java:722)
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ExceptionInInitializerError
at com.sun.javafx.css.StyleManager$StylesheetContainer.getStyleHelper(StyleManager.java:1390)
at com.sun.javafx.css.StyleManager$StylesheetContainer.access$1300(StyleManager.java:1039)
at com.sun.javafx.css.StyleManager.getStyleHelper(StyleManager.java:976)
at javafx.scene.Node.impl_createStyleHelper(Node.java:7433)
at javafx.scene.Node.impl_processCSS(Node.java:7403)
at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
at javafx.scene.Node.processCSS(Node.java:7383)
at javafx.scene.Scene.doCSSPass(Scene.java:446)
at javafx.scene.Scene.preferredSize(Scene.java:1449)
at javafx.scene.Scene.impl_preferredSize(Scene.java:1516)
at javafx.stage.Window$9.invalidated(Window.java:716)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161)
at javafx.stage.Window.setShowing(Window.java:779)
at javafx.stage.Window.show(Window.java:794)
at javafx.stage.Stage.show(Stage.java:229)
at HelloWorld.start(HelloWorld.java:30)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
... 1 more
Caused by: java.lang.NullPointerException
at com.sun.t2k.LogicalFont.<init>(LogicalFont.java:172)
at com.sun.t2k.LogicalFont.getLogicalFont(LogicalFont.java:104)
at com.sun.t2k.LogicalFont.getLogicalFont(LogicalFont.java:144)
at com.sun.t2k.T2KFontFactory.createFont(T2KFontFactory.java:356)
at com.sun.prism.j2d.J2DFontFactory.createFont(J2DFontFactory.java:38)
at com.sun.javafx.font.PrismFontLoader.loadFont(PrismFontLoader.java:399)
at javafx.scene.text.Font.<init>(Font.java:282)
at javafx.scene.text.Font.getDefault(Font.java:85)
at com.sun.javafx.css.StyleHelper.<clinit>(StyleHelper.java:1600)
... 27 more
Some other details:
Red Hat Enterprise Linux 6.2 on x64
jdk1.7.0_21
> printenv CLASSPATH
.:/usr/java/jdk1.7.0_21/jre/lib/*:/usr/java/jdk1.7.0_21/jre/lib/ext/*
There are multiple jdks installed, but I checked to be sure that both javac and java point to the jdk1.7.0_21 version.
I've search Oracle's web site, this site, and a few other Internet sites, but not found mention of this error related to JavaFX.
Upvotes: 1
Views: 2188
Reputation: 402
For those without root access, you can also add a true type font to ~/.fonts/TTF to resolve this issue.
Upvotes: 2
Reputation: 3654
If issue is the same as : https://javafx-jira.kenai.com/browse/RT-28405
Then the solution by Phil Race :
Since adding a TTF font to /usr/share/fonts works, then it seems like we are successfully using /usr/libfontconfig to locate the installed fonts, but apparently there were none to our liking. i.e. FX does not support Type 1 fonts (a policy decision), only TrueType and OpenType/CFF fonts. Are you running with OpenJDK or Oracle JDK? I'd bet an OpenJDK that wsa bundled with slackware OpenJDK has no fonts of its own, whereas OracleJDK has some of its own TrueType fonts so would not hit this problem.
The bottom line here seems to be that you should install a decent set of TrueType fonts. I think the only thing we can do on our end is improve the diagonostic when this happens.
Upvotes: 2