JAVA错误汇总

简介: 1.Slf4J-API版本兼容 问题描述 Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.helpers.

 

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

 

 

 

 

 

 

目录
相关文章
|
Java
java: 错误: 无效的源发行版:15
java: 错误: 无效的源发行版:15
682 0
java: 错误: 无效的源发行版:15
|
Java 开发工具 C++
Java调用虹软SDK的错误
Java调用虹软SDK的错误
654 0
Java程序员最容易犯的十大SQL错误,你犯过几次?
前言 Java程序员编程时需要混合面向对象思维和一般命令式编程的方法,能否完美地将两者结合起来完全得依靠编程人员的水准: 技能:任何人都能容易学会命令式编程 模式:有些人用“模式-模式”,举个例子,模式可以应用到任何地方,而且都可以归为某一类模式 心境:首先,要写个好的面向对象程序是比命令式程序难得多,你得花费一些功夫 但当Java程序员写SQL语句时,一切都不一样了。SQL是说明性语言而非面向对象或是命令式编程语言。在SQL中要写个查询语句是很简单的。但在Java里类似的语句却不容易,因为程序员不仅要反复考虑编程范式,而且也要考虑算法的问题。 下面是Java程序员在写SQL时常犯的10个错误
|
Java
JAVA初学:错误: 找不到或无法加载主类 test
程序在运行的时候具体是如何确定.class文件位置的呢?
434 0
JAVA初学:错误: 找不到或无法加载主类 test
|
算法 Java
Java数值计算易出现的10种错误
Java的数值计算容易出现很多错误,本文介绍Java数值计算易出现的10种错误。
618 0
|
Java
命令行下运行JAVA出错:错误的签名:
命令行下运行JAVA出错:错误的签名:
83 0
|
Java Android开发
关于Java/Android开发中常见异常错误汇总___知识导图分享
注: 1.若博客图片不清晰,可以鼠标点击图片查看,或者下载到电脑中查看,也可以留言给我,发你高清原图。希望对热爱学习的朋友有帮助! 2.这是汇总的关于Java/Android开发中常见的异常、错误,绘制导图如下:
149 0
关于Java/Android开发中常见异常错误汇总___知识导图分享
|
Java 数据库
记录一次Java递归调用导致java.lang.StackOverflowError错误
通过递归调用每次查询500条数据的id,再通过id删除记录的方式,数据量一大,导致递归过深,栈帧数超出虚拟栈深度,虚拟机栈过多,报java.lang.StackOverflowError错误。
558 0
记录一次Java递归调用导致java.lang.StackOverflowError错误
|
前端开发 算法 JavaScript
Java初学者一定要注意的问题,这些错误你犯过哪些?永远不要停止学习。
Java初学者一定要注意的问题,这些错误你犯过哪些?永远不要停止学习。
Java初学者一定要注意的问题,这些错误你犯过哪些?永远不要停止学习。