Welcome! Please see the About page for a little more info on how this works.

0 votes
in Java Interop by

I've been trying to use javafx.stage.Screen to capture my screen dimensions. following java documentations
here is what I wrote

(import javafx.stage.Screen)

I get the following error

Execution error (NoClassDefFoundError) at user/eval194 (REPL:1).
Could not initialize class javafx.stage.Screen

I tried using the fully qualified named and using the dot special form but I get the same error. in the docs the class screen has no constructor so I don't think I need make new instance.
java version: 1.8.0_261
repl : clj
command line: powershell 5.1.17763.1432

any idea why this happens?

1 Answer

0 votes
selected by
Best answer

JavaFX may not be included with your installation. In some cases, you have to include it explicitly, or install openjfx. If you go to java > 8, then you have to use openjfx. Also, the platform requires specific initialization and expects some things to be done on a specific event dispatch thread (relative to javafx). There are various ways to instantiate the javafx platform from another thread (e.g. the repl).

I would recommend looking at cljfx if you are interested in a bit more high level javafx programming in clojure. Javafx on Java 8 is different (some API changes here and there) from the more modern openjfx. It might be worth an upgrade if you plan to do a lot of javafx stuff.

JavaFX is included with Oracle Java 1.8, but not with Adoptopenjdk (and getting that installed is not much fun). I would recommend using Java 11+ with OpenJFX instead.
[zulu + jfx distribution](https://www.azul.com/downloads/zulu-community/?version=java-8-lts&architecture=x86-64-bit&package=jdk-fx) is what I used, and has a fairly up to date implementation of javafx (e.g. webview actually works fairly well, even rendering vega plots).  Installation amounted to unpacking the archive and setting some environment vars.  On linux there's a util called [sdkman](https://sdkman.io/usage) that makes it easier to juggle jvms.

JDK 11+ is a better bet if you can use it though; otherwise zulu (if you're living free) or oracle (if it's the company's dime).
thanks alot. I just wanted to get the screen dimensions for java.awt.Robot so cljfx would suffice
If you just need screen dimensions, then in theory awt/swing would be even easier (and definitely portable/backwards compatible).

    (import '[java.awt Toolkit Dimension])
    (defn get-screen-size []
      (let [dimension (.. Toolkit getDefaultToolkit getScreenSize)]
        {:height  (.getHeight dimension)
         :width (.getWidth dimension)}))

    user> (get-screen-size)
    {:height 2160.0, :width 3840.0}