1

I have a problem with javafx and the module-info.

I want to use the javax.validator:

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        validator.validate(dataBaseObjekt)
                .stream()
                .forEach(violation -> System.out.println(violation.getMessage())); 

Therefor I have used this dependencies:

<dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.13.Final</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.0</version>
        </dependency>

When I add these dependencies IntelliJ tells me to add: requires java.validation; to the module-info.

When I do this I get the error:

java: the unnamed module reads package javax.validation from both java.validation and jakarta.jakartaee.api

When I delete this entry from the module-info, the program is working but IntelliJ shows me an error to add this entry.

What can I do?

Thanks.

Guenni97
  • 19
  • 5
  • 1
    see f.i. https://stackoverflow.com/questions/44697738/how-to-resolve-module-reads-package-error-in-java9 to understand the error - includes a solution also but not sure if that's not outdated, would have expected that problems like these with standard libraries are resolved at the base – kleopatra May 11 '22 at 08:30
  • 1
    This may be a compatibility problem. Java EE was given to the Eclipse Foundation some time ago, and it has since been renamed Jakarta EE. This transition involved a namespace change as well (i.e., package names changed). Make sure the versions of everything you're using can work together. – Slaw May 11 '22 at 13:39

1 Answers1

2

As noted by Slaw, there was a name change from javax to jakarta. So you have to use modern versions of everything and use the jakarta namespace wherever you used to use javax.

If you have lots additional issues related to modularity in other parts of your code, it may be better for you to make your app non-modular. You can do this by deleting the module-info. Then, either reference javafx modules through command line switches or use a jdk that contains the JavaFX modules such as Bellsoft Liberica “full jdk” or Azul Zulu “jdk fx”.

Example App

Adapted from the example Hibernate Validator getting started documentation.

The current documentation also uses obsolete names, but their source repository for the tutorial has been updated to modern names. Hopefully the documentation will be fixed in the next release...

I did also update to a more modern test framework (junit 5 instead of 4).

One of the tests from the example code actually fails a test assertion (manufacturerIsNull()), as there are also some changes in the validation error messages generated by the validator framework. Semantically it is the same, but the message text has changed.

src/main/java/module-info.java

module com.example.validator {
    requires jakarta.validation;

    exports com.example.validator;
}

src/main/java/com/example/validator/Car.java

package com.example.validator;

import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

public class Car {

   @NotNull
   private String manufacturer;

   @NotNull
   @Size(min = 2, max = 14)
   private String licensePlate;

   @Min(2)
   private int seatCount;

   public Car(String manufacturer, String licencePlate, int seatCount) {
      this.manufacturer = manufacturer;
      this.licensePlate = licencePlate;
      this.seatCount = seatCount;
   }

   // getters and setters ...
}

src/test/java/com/example/validator/CarTest.java

package com.example.validator;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CarTest {

private static Validator validator;

   @BeforeAll
   public static void setUp() {
      ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
      validator = factory.getValidator();
   }

   @Test
   public void manufacturerIsNull() {
      Car car = new Car( null, "DD-AB-123", 4 );

      Set<ConstraintViolation<Car>> constraintViolations =
      validator.validate( car );

      assertEquals( 1, constraintViolations.size() );
      assertEquals(
         "may not be null",
         constraintViolations.iterator().next().getMessage()
      );
   }

   @Test
   public void licensePlateTooShort() {
      Car car = new Car( "Morris", "D", 4 );

      Set<ConstraintViolation<Car>> constraintViolations =
      validator.validate( car );

      assertEquals( 1, constraintViolations.size() );
      assertEquals(
         "size must be between 2 and 14",
         constraintViolations.iterator().next().getMessage()
      );
   }

   @Test
   public void seatCountTooLow() {
      Car car = new Car( "Morris", "DD-AB-123", 1 );

      Set<ConstraintViolation<Car>> constraintViolations =
      validator.validate( car );

      assertEquals( 1, constraintViolations.size() );
      assertEquals(
         "must be greater than or equal to 2",
         constraintViolations.iterator().next().getMessage()
      );
   }

   @Test
   public void carIsValid() {
      Car car = new Car( "Morris", "DD-AB-123", 2 );

      Set<ConstraintViolation<Car>> constraintViolations =
      validator.validate( car );

      assertEquals( 0, constraintViolations.size() );
   }
}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>validator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>validator</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.8.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>18.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>18.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>7.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>jakarta.el</artifactId>
            <version>4.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.9.0</version>
                <configuration>
                    <source>18</source>
                    <target>18</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
jewelsea
  • 150,031
  • 14
  • 366
  • 406