Java 代码中的 System.setProperty 与 ant xml 中的 <sysproperty/> 或 <jvmargs> 之间的区别

Difference between System.setProperty in Java code and <sysproperty/> or <jvmargs> in ant xml

如果我在 Java 程序代码中设置 属性,它会起作用:

try {
        System.setProperty("javax.net.ssl.trustStore", "/home/ylinghao/AllocationAnomaliesDetection/env/AllocationAnomaliesDetection-1.0/runtime/certs/InternalTrustStore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "password");
    } catch (Exception e) {
        System.out.println("Failed to specify keystore for issue" + e);
    }

但是如果我尝试在 ant 的 <jvmarg> 中设置 属性 或在 build.xml 中设置 <systproperty>,它会失败。谁能告诉我有什么区别?谢谢!

我在这里引用文档站点:

Use nested elements to specify system properties required by the class. These properties will be made available to the VM during the execution of the class (either ANT's VM or the forked VM). The attributes for this element are the same as for environment variables.

您需要 fork="true" 才能发挥 jvmargsysproperty 的效果。

你的问题是你正在构建你的应用程序。因此,您正在配置的 jvm 设置用于执行 ant 本身(或您使用的任何辅助 java 进程,例如,编译代码)。

因此在您的 ant 中,您不会影响程序的执行,这是在您启动它时在另一个 java 进程中完成的。