Tuesday, November 29, 2022

error: package javax.xml.bind does not exist

Try these fixes if you run into issues with javax.xml.bind, which was deprecated and later removed from Java.

----------------------------------------------------------------------

Solution 1 => Switch to Java 8

Please install the latest JDK 8 from oracle for your OS - https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html. If you have the latest Java (version 11/14) installed, you need to also install JDK 8 and use it to run the java code.

Switch to JDK 8 on Linux / macOS:

- Check current java version % java --version, if it is JDK 11/14 then install JDK 8

- Check all JDK versions installed % /usr/libexec/java_home -V

- Edit bash profile % nano ~/.bash_profile

export JAVA_HOME_8=$(/usr/libexec/java_home -v1.8)

export JAVA_HOME_11=$(/usr/libexec/java_home -v11)

export JAVA_HOME_14=$(/usr/libexec/java_home -v14)

# Java 8
export JAVA_HOME=$JAVA_HOME_8

# Java 11
# export JAVA_HOME=$JAVA_HOME_11

- Run this command to switch to JDK 8 - % source ~/.bash_profile

- Confirm it is JDK 8 % java -version

ref.: https://mkyong.com/java/how-to-install-java-on-mac-osx/


Switch to JDK 8 on Windows:

- Open the command prompt (cmd.exe) from Start Menu
- Check java version > "C:\Program Files\Java\jdk1.8.0_60\bin\java.exe" -version
- Compile using > "C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe" FileName.java
- Run the compiled class file using > "C:\Program Files\Java\jdk1.8.0_60\bin\java.exe" FileName

ref.: https://mkyong.com/java/how-to-set-java_home-on-windows-10/

----------------------------------------------------------------------

Solution 2 => To make the JAXB APIs available at runtime for Java 9 /10, specify the following command-line option:
--add-modules javax.xml.bind

----------------------------------------------------------------------

Solution 3 => Java 11 and later
replace javax.xml.bind with jakarta.xml.bind

This Java error appears because javax.xml.bind, the JAXB API, was deprecated in Java 9 and removed from the JDK in Java 11, so code that compiled on Java 8 can no longer find it.

You can either run on Java 8, where it is bundled, or, on newer Java, add the JAXB API and runtime as explicit dependencies through Maven or Gradle. The functionality still works once it is supplied separately.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.