خرید بک لینک

Vote count: -1

I want to use MessageDigest to get a MD5 hash, but I get an error.

import java.security.MessageDigest;

public class dn {
  public static void main(String[] args) {
    MessageDigest md = MessageDigest.getInstance("MD5");
  }
}

Error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type NoSuchAlgorithmException

Error is referring to
NoSuchAlgorithmException - if a MessageDigestSpi implementation for the specified algorithm is not available from the specified provider.
found on this site http://docs.oracle.com/javase/6/docs/api/java/security/MessageDigest.html under getInstance

I have reinstalled the latest java jdk1.7.0_21 and a different version of eclipse, but the error persists. Everything else runs fine on eclipse.

I don't know what else could i do.

asked Apr 21 '13 at 17:08

4 Answers

Vote count: 9 accepted

The error message is clear : the code doesn't compile (Unresolved compilation problem) because you're not handling the checked exception NoSuchAlgorithmException that can be thrown by MessageDigest.getInstance().

Either add this exception to the throws clause of the main method, or catch it:

public static void main(String[] args) throws NoSuchAlgorithmException {
    ...
}

or

public static void main(String[] args) {
    try {
        ...
    }
    catch (NoSuchAlgorithmException e) {
        System.err.println("I'm sorry, but MD5 is not a valid message digest algorithm");
    }
}

Note that this is a compilation error. You chose to launch your program despite the presence of compilation errors (visible in the "Problems" view of Eclipse), and despite the fact that Eclipse waed you about that before launching the program. So you tried executing code which doesn't compile, which you shouldn't do.

EDIT: fixed the typo in the code in NoSuchAlgorithmException

answered Apr 21 '13 at 17:11

Vote count: 1

You should handle the exception:

try {

   MessageDigest md = MessageDigest.getInstance("MD5");
   //...

} catch(NoSuchAlgorithmException x) {
  // do proper exception handling
}

answered Apr 21 '13 at 17:13

Vote count: 0

In addition to other answers here

Certain algorithms would not be available with some JVM

To make it truly portable application you should do this

public boolean isMDAvailable(String s)
{
    boolean success=true;
    try{MessageDigest.GetInstance(s);}
    catch(NoSuchAlgorithmException x)
    {
         success=false;
    }
    retu success;
}

Now you can get any available MD algorithm with this method

public MessageDigest getAvailableMessageDigest()
{
    if(isMDAvailable("MD5")==true)retu MessageDigest.GetInstance("MD5");
    else if(isMDAvailable("MD2")==true)retu MessageDigest.GetInstance("MD2");
    else if(isMDAvailable("SHA-512")==true)retu MessageDigest.GetInstance("SHA-512");
    else retu null;
}

answered Apr 21 '13 at 17:34

Vote count: 0

But is this exception really always needed?

They should overload the getInstance(String) method, not to throw the Exceptions for the algorithms that are required to be implemented among all Java Implementations (such as MD5 and SHA-1) to avoid this uecessary exception handling.

answered Jul 17 '14 at 13:33

برچسب: نویسنده: استخدام کار تاريخ: شنبه 25 ارديبهشت 1395 ساعت: 1:16

صفحه بندی