java - trustStore 的路径 - 设置属性不起作用? [英] java - path to trustStore - set property doesn't work?

查看:40
本文介绍了java - trustStore 的路径 - 设置属性不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个自签名证书来测试 ssl java 连接 - 但是,它拒绝定位 java trustStore.除了将类编译到的文件夹(我使用 netbeans)以及/java/jre6/bin 之外,我还在/Java/jre6/lib/security 中保存了它的副本以上似乎都不起作用,因为当我运行以下命令时 - trustStore = null.

I've setup a self-signed certificate to test an ssl java connection - however, it is refusing to locate the java trustStore. I've saved copies of it in /Java/jre6/lib/security in addition to the folder where the classes are compiled to (im using netbeans) and also to /java/jre6/bin none of the above appears to work, because when i run the following - trustStore = null.

public class ShowTrustStore {

    public static void main(String[] args) {

        System.setProperty("javax.net.ssl.keyStore", "keystore.jks");
        System.setProperty("javax.net.ssl.trustStrore", "cacerts.jks");
        System.setProperty("javax.net.ssl.keyStorePassword", "changeit");



        String trustStore = System.getProperty("javax.net.ssl.trustStore");
        if (trustStore == null) {
            System.out.println("javax.net.ssl.trustStore is not defined");
        } else {
            System.out.println("javax.net.ssl.trustStore = " + trustStore);
        }
    }
}

如何正确设置路径?

************更新************使用 getFile() 方法和更多调试数据:

**********UPDATE************ Using the getFile() method and some more debug data:

package ssltest;

public class Main {

    public static void main(String[] args) {

//        System.setProperty("javax.net.ssl.keyStore", "/keystore.jks");
//        System.setProperty("javax.net.ssl.trustStrore", "/java.home/cacerts.jks");
//        System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
//        System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

        try {
            Main.class.getResource("trustStore.jks").getFile();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String trustStore = System.getProperty("javax.net.ssl.trustStore");

        if (trustStore == null) {
            String storeLoc;
            storeLoc = System.getProperty("java.class.path");
            System.out.println("classpath: " + storeLoc);
        }

        trustStore = System.getProperty("javax.net.ssl.trustStore");
        if (trustStore == null) {
            System.out.println("javax.net.ssl.trustStore is not defined");
        } else {
            System.out.println("javax.net.ssl.trustStore = " + trustStore);
        }
    }
}

运行:java.lang.NullPointerException类路径:C:UsersMainDocumentsNetBeansProjectssslTestuildclasses;C:UsersMainDocumentsNetBeansProjectssslTestsrc在 ssltest.Main.main(Main.java:15)未定义 javax.net.ssl.trustStore构建成功(总时间:0 秒)

run: java.lang.NullPointerException classpath: C:UsersMainDocumentsNetBeansProjectssslTestuildclasses;C:UsersMainDocumentsNetBeansProjectssslTestsrc at ssltest.Main.main(Main.java:15) javax.net.ssl.trustStore is not defined BUILD SUCCESSFUL (total time: 0 seconds)

推荐答案

你打错了 - 它是 trustStore.

You have a typo - it is trustStore.

除了用System.setProperty(..)设置变量外,还可以使用

Apart from setting the variables with System.setProperty(..), you can also use

-Djavax.net.ssl.keyStore=path/to/keystore.jks

这篇关于java - trustStore 的路径 - 设置属性不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆