java - MessageDigest NoSuchAlgorithmException

标签 java

我想使用 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.htmlgetInstance

我已经重新安装了最新的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/

相关文章:

java - GNU-GhostScript-9.06 的商业用途,其许可证为 Gnu-通用公共(public)许可证 V3

java - ORA-00942 : table or view does not exist but query runs on SQL developer

java - Amazon Lambda Java 函数将 png 返回到 API 网关

java - 什么 OOP 概念可以处理以下情况?

java - 在 Lollipop 之前的设备上溢出点不会改变颜色

java - 我在创建这个 android float 气泡动画时做错了什么?

java - H2 - 一般错误 :  "java.lang.NullPointerException" [50000-182]

java - 构建失败: Cannot find an environment variable

Java教程说我可以有一个包私有(private)接口(interface),但我不能

java - 我们可以在指定目录下使用maven命令行构建源代码的war文件吗?