JAVA错误汇总

  1. 云栖社区>
  2. 博客>
  3. 正文

JAVA错误汇总

北岛知寒 2016-07-06 17:21:00 浏览441
展开阅读全文

 

1.Slf4J-API版本兼容

问题描述

Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;

最后发现是slf4j的版本不对, 他使用的是slf4j-api-2.0.99.jar

但是,但是,官方正式版只有1.6.1,而且至今我都没发现有2.0.x系列的计划呢!! 我的神啊,哪里来的版本? 经提醒,这个jar就在 1.6.1版的发行包中!! slf4j-1.6.1.zip\slf4j-1.6.1\integration\lib

神奇啊,为啥呢?? 找不到其源码jar,好吧,上JD-GUI,看看报错的类MessageFormatter.arrayFormat 2.0.99版的方法声明:

public static final String arrayFormat(String messagePattern, Object[] argArray)

1.6.1版的方法声明:

public static final FormattingTuple arrayFormat(String messagePattern, Object[] argArray)

看到区别了吧,返回值的类型不同了!同样的,还有一个1.6.99版的API,内容与2.0.99版一致

继续追查, 看Slf4j源码仓库的一个提交:https://github.com/ceki/slf4j/commit/581b20f8aaa140ce722669a2cf1d8582a963ff4e 里面的comments是: `

- fixed bug 154

It is expected that all SLF4J releases in the 1.6.x and 2.0.x series will be mutually compatible. Note that the 2.0.x series is expected to require JDK 1.5 or later. `

兼容旧版本?? 明显就是不兼容嘛! 但 bug 154 到底是什么内容呢?

几经查找,终于找到其issue系统的地址,是个bugzilla呢 -- 天啊,官网一个网站,下载一个网站,源码管理一个网站,issue管理又是再一个网站?!!

http://bugzilla.slf4j.org/show_bug.cgi?id=154 但内容感觉与 2.0.x无关呢.

解决方案

将slf4j升级到1.6.5以上。

2.解决PKIX:unable to find valid certification path to requested target 的问题

这两天在twitter服务器上忽然遇到这样的异常:

e: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 

经过检查确认,完整的异常信息应该如下:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

后来到twitter开发者网站上去看到了也有人遇到了类似的问题,链接如下:https://dev.twitter.com/discussions/533

但是都没说明具体的解决方法,郁闷啊!!

在oracle某一博客上说是可以这样解决的:http://blogs.oracle.com/gc/entry/unable_to_find_valid_certification

但是悲催的是那个Java类已经无法下载了。。。。历经N次的google,终于找到该文件了,不敢独享,记录于此。

 

问题的根本是:

缺少安全证书时出现的异常。

解决问题方法:

将你要访问的webservice/url....的安全认证证书导入到客户端即可。

 

以下是获取安全证书的一种方法,通过以下程序获取安全证书(记得根据需求修改部分代码):

package com.company;

import javax.net.ssl.*;
import java.io.*;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class InstallCert {

    private final static String PRODUCTION_HOST = "gateway.push.apple.com";
    private final static int PRODUCTION_PORT = 2195;
    private final static String DEVELOPMENT_HOST = "gateway.sandbox.push.apple.com";
    private final static int DEVELOPMENT_PORT = 2195;

    private static String CLIENT_KEY_STORE = "C:\\myfile\\certificates\\pair1\\push-pro1-123456.p12";

    public static void main(String[] args) throws Exception {

        String host;
        int port;
        host = DEVELOPMENT_HOST;
        port = DEVELOPMENT_PORT;
        String passwd = "123456";

        File file = new File(CLIENT_KEY_STORE);
        System.out.println("Loading KeyStore " + file + "...");
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(new FileInputStream(file), passwd.toCharArray());

        SSLContext context = SSLContext.getInstance("TLS");
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];
        SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
        context.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory factory = context.getSocketFactory();
        System.out.println("Opening connection to " + host + ":" + port + "...");
        SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
        socket.setSoTimeout(10000);
        try {
            System.out.println("Starting SSL handshake...");
            socket.startHandshake();
            socket.close();
            System.out.println();
            System.out.println("No errors, certificate is already trusted");
        } catch (SSLException e) {
            System.out.println();
            e.printStackTrace(System.out);
        }

        X509Certificate[] chain = tm.chain;
        if (chain == null) {
            System.out.println("Could not obtain server certificate chain");
            return;
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Server sent " + chain.length + " certificate(s):");
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        for (int i = 0; i < chain.length; i++) {
            X509Certificate cert = chain[i];
            System.out.println(" " + (i + 1) + " Subject " + cert.getSubjectDN());
            System.out.println("   Issuer  " + cert.getIssuerDN());
            sha1.update(cert.getEncoded());
            System.out.println("   sha1    " + toHexString(sha1.digest()));
            md5.update(cert.getEncoded());
            System.out.println("   md5     " + toHexString(md5.digest()));
            System.out.println();
        }

        System.out.println("Enter certificate to add to trusted keystore or 'q' to quit: [1]");
        String line = reader.readLine().trim();
        int k;
        try {
            k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1;
        } catch (NumberFormatException e) {
            System.out.println("KeyStore not changed");
            return;
        }

        X509Certificate cert = chain[k];
        String alias = host + "-" + (k + 1);
        ks.setCertificateEntry(alias, cert);

        OutputStream out = new FileOutputStream("apnssandbox.p12");
        ks.store(out, passwd.toCharArray());
        out.close();

        System.out.println();
        System.out.println(cert);
        System.out.println();
        System.out
                .println("Added certificate to keystore 'jssecacerts' using alias '"
                        + alias + "'");
    }

    private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();

    private static String toHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 3);
        for (int b : bytes) {
            b &= 0xff;
            sb.append(HEXDIGITS[b >> 4]);
            sb.append(HEXDIGITS[b & 15]);
            sb.append(' ');
        }
        return sb.toString();
    }

    private static class SavingTrustManager implements X509TrustManager {

        private final X509TrustManager tm;
        private X509Certificate[] chain;

        SavingTrustManager(X509TrustManager tm) {
            this.tm = tm;
        }

        public X509Certificate[] getAcceptedIssuers() {
            throw new UnsupportedOperationException();
        }

        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            throw new UnsupportedOperationException();
        }
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            this.chain = chain;
            tm.checkServerTrusted(chain, authType);
        }
    }

}
View Code

输入1,回车,然后会在当前的目录下产生一个名为“ssecacerts”的证书。

将证书拷贝到$JAVA_HOME/jre/lib/security目录下,或者通过以下方式:
System.setProperty("javax.net.ssl.trustStore", "你的jssecacerts证书路径");

 

3.

Maven在部署(deploy)的时候会报错:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project ams-httpdns: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
View Code

 

 

 

 

 

 

网友评论

登录后评论
0/500
评论
北岛知寒
+ 关注