8

I have the following code to generate an SHA-1 hash.

@Override
    public String generateHash(String plainTextPassword) {

        String hashedPassword = "";
        try {
            MessageDigest msdDigest = MessageDigest.getInstance("SHA-1");
            msdDigest.update(plainTextPassword.getBytes("UTF-8"), 0, plainTextPassword.length());
            hashedPassword = DatatypeConverter.printHexBinary(msdDigest.digest());
        } catch (Exception e) {
            System.out.println("HASHING FAILED");
        }
        return hashedPassword;
    }

On my local machine, I have no problem using the DatatypeConverter class. The package is javax.xml.bind.DatatypeConverter; When I transfer my project over to a linux machine running Ubuntu, the DatatypeConverter class is not resolved.

  • 1
    Might this help you? https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j Maybe you are using different versions of Java on different maschines? – CodingTil Apr 08 '20 at 16:10
  • 1
    I'd try and stay away from that class anyway. The `DataTypeConverter` class is part of an XML framework. It's not supposed to be used as general converter. And it is relatively easy to program this yourself or use Apache Commons or Google Guava. And yes, it is annoying that this functionality is not provided, which I'm going to try and resolve right away. – Maarten Bodewes Apr 08 '20 at 16:19
  • 1
    As noted by @CodingTil, the JAXB classes were removed from Java SE for Java 11+. – President James K. Polk Apr 08 '20 at 16:29
  • 2
    See the answers in https://stackoverflow.com/questions/19450452/how-to-convert-byte-array-to-hex-format-in-java for alternative ways to convert bytes to a hex string. – VGR Apr 08 '20 at 17:24

1 Answers1

16

The module javax.xml.bind has been put outside the jdk as of Java 9.

Also, as part of the donation of Java EE to the Eclipse Foundation, its new name is Jakarta EE and accordingly the namespace was renamed to Jakarta. So you have to make the following modifications :

Add the dependency to your project :

With Maven, add the following to your pom.xml :

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>3.0.0</version>
</dependency>

With Gradle, add the following to your build.gradle :

implementation group: 'jakarta.xml.bind', name: 'jakarta.xml.bind-api-parent', version: '3.0.0', ext: 'pom'

And, in your java code where the dependence is used, change the import to :

import jakarta.xml.bind.DatatypeConverter;
Pierre C
  • 2,920
  • 1
  • 35
  • 35