我正在为PEMParser.java类和oracle java 7使用bcpkix-jdk15on-151.jar,
但是在行
PEMParser pemRd = new PEMParser(fRd);
我得到了以下的例外
Exception in thread "main" java.lang.VerifyError: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Z
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.bouncycastle.openssl.PEMParser.<init>(Unknown Source)
at com.sarang.test.CertificateLoader.getCertificate(CertificateLoader.java:24)
at com.sarang.test.CertificateLoader.main(CertificateLoader.java:43)
我的源代码是:
public static X509Certificate getCertificate() throws IOException {
String pemfile = "/home/sarang07/keys/key1.crt";
java.security.cert.X509Certificate cert = null;
try {
FileReader fRd = new FileReader(pemfile);
PEMParser pemRd = new PEMParser(fRd);
Object o;
o = pemRd.readPemObject();
if (o instanceof java.security.cert.X509Certificate)
{
cert = (java.security.cert.X509Certificate) o;
} else {
throw new IOException("fail to load certificate from file " + pemfile);
}
} catch (FileNotFoundException e) {
throw new IOException("Can't find file " + pemfile);
}
return cert;
}
答案 0 :(得分:1)
您可能存在依赖性冲突 - 你在路径中有2个版本的上述jar吗? 也许你是在POM文件中添加依赖项,还是在你的构建中添加了另一个jar的veriosn?
答案 1 :(得分:0)
最后我从下面的代码片段中得到了解决方案...
public static java.security.cert.X509Certificate getCertificate(String pemfile) throws Exception
{
java.security.cert.X509Certificate cert = null;
try {
FileReader fRd = new FileReader(pemfile);
final PemReader certReader = new PemReader(fRd);
final PemObject certAsPemObject = certReader.readPemObject();
if (!certAsPemObject.getType().equalsIgnoreCase("CERTIFICATE")) {
throw new Exception("Certificate file does not contain a certificate but a " + certAsPemObject.getType());
}
final byte[] x509Data = certAsPemObject.getContent();
final CertificateFactory fact = CertificateFactory.getInstance("X509");
cert = (X509Certificate) fact.generateCertificate(new ByteArrayInputStream(x509Data));
if (!(cert instanceof X509Certificate)) {
throw new Exception("Certificate file does not contain an X509 certificate");
}
} catch (FileNotFoundException e) {
throw new IOException("Can't find file " + pemfile);
}
return cert;
}