在java中,在运行时递归更改系统属性

在java中,在运行时递归更改系统属性,java,runtime,keystore,truststore,system-properties,Java,Runtime,Keystore,Truststore,System Properties,我有一个问题,正在搜索一个在java中运行时更改系统属性的示例。换句话说,我有一个独立的库,它将加载System.setProperty(“javax.net.ssl.trustStore”,trustStorePath),其中trustStorePath的值将根据条件变化。如果条件更改,则需要更改trustStorePath的值,并需要设置系统属性 但问题是,当我第一次设置值时,它存储并使用该值,即使我更改了trustStorePath的值并再次设置了系统属性。这一变化没有反映出来 那么,我该

我有一个问题,正在搜索一个在java中运行时更改系统属性的示例。换句话说,我有一个独立的库,它将加载
System.setProperty(“javax.net.ssl.trustStore”,trustStorePath)
,其中trustStorePath的值将根据条件变化。如果条件更改,则需要更改trustStorePath的值,并需要设置系统属性

但问题是,当我第一次设置值时,它存储并使用该值,即使我更改了trustStorePath的值并再次设置了系统属性。这一变化没有反映出来

那么,我该怎么做呢。下面是相同的示例代码段

        if (getFile(keyStorePath).exists()  && isChanged ) {
                System.setProperty("javax.net.ssl.keyStore", keyStorePath);
                System.setProperty("javax.net.ssl.keyStoreType", "JKS");
                System.setProperty("javax.net.ssl.keyStorePassword", Pwd);
        }else if (getFile(testMerchantKeyStorePath).exists() ) {
            System.setProperty("javax.net.ssl.keyStore", testMerchantKeyStorePath);
                System.setProperty("javax.net.ssl.keyStoreType", "JKS");
                System.setProperty("javax.net.ssl.keyStorePassword",Pwd);

    }

听起来您想使用动态信任存储。您可以在打开任何连接之前执行此操作:

    KeyStore ts = KeyStore.getInstance("JKS");
    ts.load(new FileInputStream(new File("Your_New_Trust_Store_Path")), "password".toCharArray());

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ts);

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, tmf.getTrustManagers(), null);

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

    // Open Connection .... etc. ....
您可以在每次更改您的
trustStorePath
时执行此操作。

嘿,