我已经设置了一个自签名证书来测试 ssl java 连接 - 但是,它拒绝找到 java trustStore。除了将类编译到的文件夹(我使用netbeans)和/java/jre6/bin之外,我还在/Java/jre6/lib/security中保存了它的副本,因为以上都不起作用,因为当我运行以下命令时 - 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() 方法和更多调试数据:
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:\Users\Main\Documents\NetBeansProjects\sslTest\build\classes;C:\Users\Main\Documents\NetBeansProjects\sslTest\src 在 ssltest.Main.main(Main.java :15) 未定义 javax.net.ssl.trustStore 构建成功(总时间:0 秒)