1

I am using Pair data structure in my code with java 8, it runs fine in local. But in jenkins build, I get this issue:

error: package javafx.util does not exist
import javafx.util.Pair;
  • 1
    Which operating system is the Jenkins build running on? Which JDK distribution is being used in the Jenkins build? Is JavaFX included in the JDK (not guaranteed, even for Java 8)? – Slaw Feb 17 '23 at 04:51
  • 1
    Probably you have OpenJDK 8 or a more recent JDK version on the Jenkins machine, but Oracle JDK 8 on your local development machine. – jewelsea Feb 17 '23 at 05:05
  • JDK 8 and JavaFX 8 are largely obsolete. I’d recommend a modern version (19+). Info on that is at [openjfx.io](https://openjfx.io/). – jewelsea Feb 17 '23 at 05:07
  • 1
    If the only reason you are using JavaFX is for the `Pair` class, then [use something else](https://stackoverflow.com/questions/521171/a-java-collection-of-value-pairs-tuples). `Pair` (and other tuples like triplets, etc) were deliberately not put in the standard Java SE libraries, see the [record alternatives section](https://openjdk.org/jeps/395) for reasons why. – jewelsea Feb 17 '23 at 05:15
  • @jewelsea Yes, I had to use the other library as the javafx pair didn't work – GlidingSwords997 Feb 18 '23 at 05:44

1 Answers1

4

Your code uses the Pair class that comes with JavaFX. Unlike Swing, JavaFX has never been a standard part of Java SE. You must make an implementation of JavaFX available to your app.

It would seem that your local machine has an implementation of JavaFX, but your Jenkins machine does not. You need to get an implementation of JavaFX onto that Jenkins machine.

The OpenJFX subproject on the OpenJDK project is an open-source implementation of JavaFX. The Gluon company leads OpenJFX, in cooperation with Oracle.

One solution is for you to bundle OpenJFX as part of your app. The common way to do this is to configure a dependency management tool such as Apache Maven or Gradle.

An alternative solution is to use a JDK that comes bundled with an implementation of JavaFX.

Vendors of JDK products are free to bundle an implementation of JavaFX.

Oracle chose to do so with early releases of their commercial Java 8 product. The company ceased bundling in later releases of Java 8. I guess that your local machine has an earlier release of Java 8 while your Jenkins machine has a later release of Java 8, with and without JavaFX bundled respectively.

At least two JDK vendors currently bundle OpenJFX with some editions of their JDK products:

  • Azul Systems provides ZuluFX
  • BellSoft provides LibericaFX.

Of course, going this route means you must also get OpenJFX onto your app’s deployment machines.


As commented, if you are not really making a JavaFX app, then bundling OpenJFX just for that Pair class is overkill.

Some folks use the map entry interface and implementations as a pairing class. See the Map.Entry interface with links to the mutable and immutable concrete classes.

I suggest you roll your own.

The easiest DIY implementation in Java 16+ is as a record.

record PairOfInts ( int x , int y ) {}

Tip: You can declare a record locally within a method, in addition to nested in a class or standing alone as its own class.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 2
    “*JavaFX has never been a standard part of Java*” but it shipped with the reference implementation and there was the claim that it was supposed to replace Swing, which [you can still read on the Oracle website](https://www.oracle.com/java/technologies/javafx/faq-javafx.html#:~:text=Is%20JavaFX%20replacing%20Swing,Yes), in undisputed form. So how could anyone back then assume that it is not a standard part of Java? Though, I’d never have considered using a class from the depth of JavaFX for creating a simple pair. That’s not better than using, e.g. `AbstractMap.Simple[Immutable]Entry`… – Holger Feb 17 '23 at 08:16
  • [Java v1.8.0_361](https://www.oracle.com/java/technologies/downloads/#java8-linux), which bundles JavaFX v8.0.361-b09, may an alternative. – trashgod Feb 17 '23 at 13:20