PostConstruct and PreDestroy annotation errors in Java 9

I am trying to use annotations with two methods in Spring in Java 9.

import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; . . . @PostConstruct public void myFoo() { System.out.println("postconstruct - foo"); } @PreDestroy public void myFoo2() { System.out.println("predestroy - foo2"); } 

I get the following errors:

 Error:(7, 13) java: package javax.annotation is not visible (package javax.annotation is declared in module java.xml.ws.annotation, which is not in the module graph) Error:(8, 13) java: package javax.annotation is not visible (package javax.annotation is declared in module java.xml.ws.annotation, which is not in the module graph) 

Why am I getting an error message? The result of the examples works well in Java 8. Is there a solution?

+5
source share
3 answers

As you know, annotations are in the java.xml.ws.annotation module. Since it is a Java EE module, it is deprecated for deletion in Java 9 and is not enabled by default , so you need to add it manually with --add-modules .

In Java 11, the module will completely disappear, and --add-modules will fail because java.xml.ws.annotation no longer exists. The best solution is to immediately replace it with a third-party addiction. Use Java Commons Annotations for this, it is available on Maven Central:

 <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.1</version> </dependency> 
+6
source

Java 9 removed javax.annotation from its default class. That is why you get an error.

What you can do is download the required jar file from here . And add it to your build path.

Assuming you are using eclipse:

  • Copy the jar file that you downloaded to the lib folder of your project.
  • Right click on project, select Properties
  • On the left side, click Java Build Path
  • In the upper center of the dialog, click Libraries
  • Click Classpath , and then click Add JAR
  • Browse to the lib / javax.annotation-api-1.2.jar JAR file
  • Click OK, then Apply and Close
0
source

First, I check if the module exists.

 Soners-MacBook-Pro:~ soner$ java --list-modules java.activation@9.0.1 java.base@9.0.1 java.compiler@9.0.1 java.corba@9.0.1 java.datatransfer@9.0.1 java.desktop@9.0.1 java.instrument@9.0.1 java.jnlp@9.0.1 java.logging@9.0.1 java.management@9.0.1 java.management.rmi@9.0.1 java.naming@9.0.1 java.prefs@9.0.1 java.rmi@9.0.1 java.scripting@9.0.1 java.se@9.0.1 java.se.ee@9.0.1 java.security.jgss@9.0.1 java.security.sasl@9.0.1 java.smartcardio@9.0.1 java.sql@9.0.1 java.sql.rowset@9.0.1 java.transaction@9.0.1 java.xml@9.0.1 java.xml.bind@9.0.1 java.xml.crypto@9.0.1 java.xml.ws@9.0.1 ---- java.xml.ws.annotation@9.0.1 ---- javafx.base@9.0.1 javafx.controls@9.0.1 javafx.deploy@9.0.1 javafx.fxml@9.0.1 javafx.graphics@9.0.1 javafx.media@9.0.1 javafx.swing@9.0.1 javafx.web@9.0.1 jdk.accessibility@9.0.1 jdk.attach@9.0.1 jdk.charsets@9.0.1 jdk.compiler@9.0.1 jdk.crypto.cryptoki@9.0.1 jdk.crypto.ec@9.0.1 jdk.deploy@9.0.1 jdk.deploy.controlpanel@9.0.1 jdk.dynalink@9.0.1 jdk.editpad@9.0.1 jdk.hotspot.agent@9.0.1 jdk.httpserver@9.0.1 jdk.incubator.httpclient@9.0.1 jdk.internal.ed@9.0.1 jdk.internal.jvmstat@9.0.1 jdk.internal.le@9.0.1 jdk.internal.opt@9.0.1 jdk.internal.vm.ci@9.0.1 jdk.jartool@9.0.1 jdk.javadoc@9.0.1 jdk.javaws@9.0.1 jdk.jcmd@9.0.1 jdk.jconsole@9.0.1 jdk.jdeps@9.0.1 jdk.jdi@9.0.1 jdk.jdwp.agent@9.0.1 jdk.jfr@9.0.1 jdk.jlink@9.0.1 jdk.jshell@9.0.1 jdk.jsobject@9.0.1 jdk.jstatd@9.0.1 jdk.localedata@9.0.1 jdk.management@9.0.1 jdk.management.agent@9.0.1 jdk.management.cmm@9.0.1 jdk.management.jfr@9.0.1 jdk.management.resource@9.0.1 jdk.naming.dns@9.0.1 jdk.naming.rmi@9.0.1 jdk.net@9.0.1 jdk.pack@9.0.1 jdk.packager@9.0.1 jdk.packager.services@9.0.1 jdk.plugin@9.0.1 jdk.plugin.dom@9.0.1 jdk.plugin.server@9.0.1 jdk.policytool@9.0.1 jdk.rmic@9.0.1 jdk.scripting.nashorn@9.0.1 jdk.scripting.nashorn.shell@9.0.1 jdk.sctp@9.0.1 jdk.security.auth@9.0.1 jdk.security.jgss@9.0.1 jdk.snmp@9.0.1 jdk.unsupported@9.0.1 jdk.xml.bind@9.0.1 jdk.xml.dom@9.0.1 jdk.xml.ws@9.0.1 jdk.zipfs@9.0.1 oracle.desktop@9.0.1 oracle.net@9.0.1 

Yes there is. Now you need to set the --add-modules parameter in the IDE (e.g. IDEA in my case). I add --add-modules java.xml.ws.annotation

enter image description here enter image description here

Booyaka, it works like a charm!

-1
source

Source: https://habr.com/ru/post/1275436/


All Articles