我想使用 MessageDigest
获取 MD5 哈希值,但出现错误。
import java.security.MessageDigest;
public class dn {
public static void main(String[] args) {
MessageDigest md = MessageDigest.getInstance("MD5");
}
}
错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type NoSuchAlgorithmException
错误指的是
NoSuchAlgorithmException - 如果指定的提供程序无法提供指定算法的 MessageDigestSpi 实现。
在此网站上找到http://docs.oracle.com/javase/6/docs/api/java/security/MessageDigest.html在getInstance
我已经重新安装了最新的java jdk1.7.0_21和不同版本的eclipse,但错误仍然存在。其他一切在 eclipse 上运行良好。
我不知道还能做什么。
最佳答案
错误消息很明确:代码无法编译( Unresolved 编译问题),因为您没有处理已检查的异常NoSuchAlgorithmException
可以通过 MessageDigest.getInstance()
抛出。
要么将此异常添加到 main 方法的 throws 子句中,要么捕获它:
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");
}
}
请注意,这是一个编译错误。尽管存在编译错误(在 Eclipse 的“问题” View 中可见),并且 Eclipse 在启动程序之前警告过您这一点,您还是选择启动程序。因此,您尝试执行无法编译的代码,这是您不应该执行的。
编辑: 修复了 NoSuchAlgorithmException 中代码中的拼写错误
关于java - MessageDigest NoSuchAlgorithmException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16133881/