{"id":10836,"date":"2023-04-20T13:26:53","date_gmt":"2023-04-20T05:26:53","guid":{"rendered":"http:\/\/123.57.164.21\/?p=10836"},"modified":"2023-04-20T13:41:09","modified_gmt":"2023-04-20T05:41:09","slug":"java%e4%b8%ad%e7%9a%84system-getenv%e5%92%8csystem-getproperty","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=10836","title":{"rendered":"Java\u4e2d\u7684System.getenv()\u548cSystem.getProperty()"},"content":{"rendered":"\n<p>System.getenv()\u548cSystem.getProperty()\u662fJava\u79cd\u5e38\u7528\u7684\u4e24\u79cd\u8bfb\u53d6\u73af\u5883\u914d\u7f6e\u4fe1\u606f\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u90fd\u662fjava.lang.<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=System%E7%B1%BB&amp;spm=1001.2101.3001.7020\" target=\"_blank\">System\u7c7b<\/a>\u7684\u65b9\u6cd5\uff0c\u7528\u9014\u6709\u76f8\u4f3c\u4e4b\u5904\uff0c\u4f46\u6211\u4eec\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u9700\u8981\u66f4\u52a0\u6ce8\u610f\u4e8c\u8005\u7684\u533a\u522b\u3002<\/p>\n\n\n\n<p><strong>1. System.getenv()<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>System.getenv()\u8bfb\u53d6\u7684\u662f\u5f53\u524d\u7cfb\u7edf\u73af\u5883\u7684\u73af\u5883\u53d8\u91cf\u3002\u8981\u6df1\u5165\u7406\u89e3\u73af\u5883\u53d8\u91cf\u7684\u672c\u8d28\uff0c\u9700\u8981\u5bf9\u7cfb\u7edf\u7236\u5b50\u8fdb\u7a0b\u5173\u7cfb\u6709\u66f4\u52a0\u6e05\u6670\u7684\u8ba4\u8bc6\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5b50\u8fdb\u7a0b\u5728\u521b\u5efa\u65f6\uff0c\u4f1a\u5c06\u7236\u8fdb\u7a0b\u5f53\u65f6\u7684\u73af\u5883\u53d8\u91cf\u590d\u5236\u4e00\u4efd\uff0c\u800c\u7236\u8fdb\u7a0b\u4e2d\u4e4b\u540e\u53d1\u751f\u7684\u73af\u5883\u53d8\u91cf\u7684\u53d8\u52a8\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5b50\u8fdb\u7a0b\u4e2d\u5bf9\u5e94\u7684\u73af\u5883\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\u5f53\u4e0d\u4f20\u5165\u53c2\u6570\u65f6\uff0cSystem.getenv()\u7684\u8fd4\u56de\u503c\u4e3a\u4e00\u4e2a\u53ea\u8bfb\u7684Map\uff0c\u5176\u4e2d\u5305\u62ec\u5f53\u524d\u73af\u5883\u4e0b\u6240\u6709\u73af\u5883\u53d8\u91cf\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6253\u5370\u51fa\u8fd9\u6761\u8bed\u53e5\u6267\u884c\u65f6\u73af\u5883\u4e2d\u6240\u6709\u7684\u73af\u5883\u53d8\u91cf\u548c\u5b83\u4eec\u5bf9\u5e94\u7684\u503c\uff0c\u5728 windows \u4e2d\u662f \u7528\u6237\u73af\u5883\u53d8\u91cf\u548c\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c<br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-261-1024x545.png\" alt=\"\" class=\"wp-image-10837\" width=\"402\" height=\"214\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-261-1024x545.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-261-300x160.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-261-768x409.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-261-830x442.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-261-230x123.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-261-350x186.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-261-480x256.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-261.png 1378w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-262-1024x577.png\" alt=\"\" class=\"wp-image-10838\" width=\"414\" height=\"233\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-262-1024x577.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-262-300x169.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-262-768x432.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-262-830x467.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-262-230x130.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-262-350x197.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-262-480x270.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-262.png 1332w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">System.getenv().forEach((key, value) -> {\n    System.out.println(key + \":\" + value);\n});<\/pre>\n\n\n\n<p>\u6ce8\u610f\uff1a\u901a\u8fc7 <code>System.getenv()<\/code> \u83b7\u53d6\u7684 Map \u662f\u4e0d\u652f\u6301\u4fee\u6539\u7684\uff0c\u4e3a\u4e86\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff0c\u6211\u4eec\u719f\u6089\u4e0b\u8fd9\u5757\u7684\u4ee3\u7801<\/p>\n\n\n\n<p>\u5f53\u4e0d\u4f20\u5165\u53c2\u6570\u65f6\uff0c<code>System.getenv()<\/code>\u7684\u8fd4\u56de\u503c\u4e3a\u4e00\u4e2a<strong>\u53ea\u8bfb<\/strong>\u7684<code>Map<\/code>\uff0c\u5176\u4e2d\u5305\u62ec\u5f53\u524d\u73af\u5883\u4e0b\u6240\u6709\u73af\u5883\u53d8\u91cf\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6253\u5370\u51fa\u8fd9\u6761\u8bed\u53e5\u6267\u884c\u65f6\u73af\u5883\u4e2d\u6240\u6709\u7684\u73af\u5883\u53d8\u91cf\u548c\u5b83\u4eec\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static java.util.Map&lt;String,String> getenv() {\n      \/\/ ......\n      return ProcessEnvironment.getenv();\n  }\n<\/pre>\n\n\n\n<p><code>ProcessEnvironment.getenv()<\/code> \u8be5\u65b9\u6cd5\u7684\u6838\u5fc3\u903b\u8f91\u5982\u4e0b\uff0c\u4ece\u4ee5\u4e0b\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u8fd4\u56de\u7684 map \u662f\u901a\u8fc7<code>Collections.unmodifiableMap()<\/code> \u5904\u7406\u7684\uff0c\u56e0\u6b64\uff0c\u8fd4\u56de\u7684map \u662f\u4e0d\u80fd\u88ab\u4fee\u6539\u7684\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">final class ProcessEnvironment extends HashMap&lt;String,String>{\n    private static final Map&lt;String,String> theUnmodifiableEnvironment;\n    static {\n        theEnvironment  = new ProcessEnvironment();\n\t\ttheUnmodifiableEnvironment\n           \t = Collections.unmodifiableMap(theEnvironment);\n    }\n    \/\/ Only for use by System.getenv()\n    static Map&lt;String,String> getenv() {\n        return theUnmodifiableEnvironment;\n    }\n}\n<\/pre>\n\n\n\n<p>\u4e00\u822c\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u65f6\uff0c\u4e00\u822c\u4e0d\u4f1a\u83b7\u53d6\u5168\u90e8\u7684\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\uff0c\u56e0\u800c\u53ef\u4ee5\u901a\u8fc7 <code>System.getEnv(key)<\/code> \u83b7\u53d6\u5bf9\u5e94\u73af\u5883\u53d8\u91cf\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static String getenv(String name) {\n     \/\/ .....\n     return ProcessEnvironment.getenv(name);\n }<\/pre>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\u6709\uff1aSystem.getenv(String key);<br>\u90a3\u4e48\u5982\u4f55\u5b9e\u73b0System.setenv\uff08\uff09\uff1f<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-263-1024x662.png\" alt=\"\" class=\"wp-image-10840\" width=\"469\" height=\"303\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-263-1024x662.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-263-300x194.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-263-768x497.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-263-1536x993.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-263-830x537.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-263-230x149.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-263-350x226.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-263-480x310.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-263.png 1788w\" sizes=\"(max-width: 469px) 100vw, 469px\" \/><\/figure><\/div>\n\n\n\n<p>\u5c1d\u8bd5\u4fee\u6539\u8fd9\u4e00<code>Map<\/code>\u7684\u5185\u5bb9\u5c06\u4f1a\u629b\u51fa<code>UnsupportedOperationException<\/code>\u5f02\u5e38\u3002<\/p>\n\n\n\n<p><code>System.getenv()<\/code>\u4e5f\u53ef\u4ee5\u63a5\u53d7\u5355\u4e2a<code>String<\/code>\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\uff0c\u8868\u793a\u9700\u8981\u83b7\u53d6\u7684\u73af\u5883\u53d8\u91cf\u540d\u3002\u82e5\u5728\u5f53\u524d\u73af\u5883\u4e2d\u5b58\u5728\u8fd9\u4e00\u53d8\u91cf\uff0c\u5219\u4ee5<code>String<\/code>\u65b9\u5f0f\u8fd4\u56de\u5176\u5bf9\u5e94\u7684\u503c\uff0c\u5426\u5219\u8fd4\u56de<code>null<\/code>\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528<code>System.getenv()<\/code>\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5904\u7406\u8fd4\u56de<code>null<\/code>\u7684\u60c5\u5f62\uff0c\u5426\u5219\u5c06\u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4<code>NullPointerException<\/code>\u5f02\u5e38\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>2. System.getProperty()<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u901a\u4e0a\uff0c\u901a\u8fc7 <code>System.getProperty(key)<\/code>\u83b7\u53d6\u5355\u4e2a\u53d8\u91cf\u503c\uff0c\u901a\u8fc7<code>System.getPropertys()<\/code> \u83b7\u53d6\u6240\u6709 JVM \u73af\u5883\u53d8\u91cf\u503c\u3002\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">System.getProperties().forEach((k,v) -> {\n    System.out.println(k + \"=\" + v);\n});<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5217\u51fa\u4e00\u4e9b\u5e38\u89c1\u7684<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ classpath\nsun.boot.class.path=C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\resources.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\lib\\rt.jar;....\njava.library.path=C:\\Program Files\\Java\\jdk1.8.0_131\\bin;C:\\WINDOWS\\Sun\\Java\\bin;C:\\WINDOWS\\system32;\nfile.separator=\\\nsun.jnu.encoding=GBK\njava.version=1.8.0_131\njava.vm.info=mixed mode\n<\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u901a\u8fc7 \u542f\u52a8JVM\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 -D \u8bbe\u7f6eJVM \u73af\u5883\u53d8\u91cf\u503c\uff0c\u5728\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\uff0c<code>System.setProperty(key,value)<\/code> \u8bbe\u7f6eJVM \u73af\u5883\u53d8\u91cf\uff0c\u8be5\u503c\u5728\u6574\u4e2aJVM \u95f4\u662f\u5171\u4eab\u7684\u3002<\/p>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Java\u63d0\u4f9b\u4e86System\u7c7b\u7684\u9759\u6001\u65b9\u6cd5getenv()\u548cgetProperty()\u7528\u4e8e\u8fd4\u56de\u7cfb\u7edf\u76f8\u5173\u7684\u53d8\u91cf\u4e0e\u5c5e\u6027\uff0cgetenv\u65b9\u6cd5\u8fd4\u56de\u7684\u53d8\u91cf\u5927\u591a\u4e8e\u7cfb\u7edf\u76f8\u5173\uff0cgetProperty\u65b9\u6cd5\u8fd4\u56de\u7684\u53d8\u91cf\u5927\u591a\u4e0ejava\u7a0b\u5e8f\u6709\u5173\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7 System.getenv() \u548c getProperty() \u53ef\u4ee5\u7b80\u5316\u6211\u4eec\u7684\u6709\u4e9b\u914d\u7f6e\uff0c\u76f8\u6bd4\u4e8e xml \u7b49\u914d\u7f6e\u6587\u4ef6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u66f4\u8f7b\u91cf\uff0c\u5982\u679c\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\u6ca1\u6709\u914d\u7f6e\u6587\u4ef6\uff0c\u4f46\u60f3\u4f7f\u67d0\u4e00\u4e24\u9879\u529f\u80fd\u5b9e\u73b0\u53ef\u914d\u7f6e\uff0c\u901a\u8fc7System.getenv() \u548c getProperty()\u53ef\u4ee5\u975e\u5e38\u5bb9\u6613\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u7ecf\u8fc7\u4e0a\u8ff0\u6bd4\u8f83\uff0c\u53ef\u4ee5\u5c06<code>System.getenv()<\/code>\u548c<code>System.getProperty()<\/code>\u7684\u533a\u522b\u5f52\u7eb3\u4e3a\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ul><li><code>System.getProperty()<\/code>\u6240\u83b7\u53d6\u7684<code>Property<\/code>\u662f\u53ef\u8bfb\u53ef\u5199\u7684\uff0c\u800c<code>System.getenv()<\/code>\u83b7\u53d6\u7684\u73af\u5883\u53d8\u91cf\u53ea\u53ef\u8bfb\u4e0d\u53ef\u5199\u3002<\/li><li><code>System.getProperty()<\/code>\u8981\u6c42\u6211\u4eec\u63d0\u4f9b\u9ed8\u8ba4\u9879\uff0c\u800c\u4f7f\u7528<code>System.getenv()<\/code>\u65f6\u9700\u8981\u8003\u8651<code>null<\/code>\u7684\u60c5\u51b5<\/li><li><code>Property<\/code>\u53ea\u5bf9Java\u7a0b\u5e8f\u6709\u5f71\u54cd\uff0c\u800c\u73af\u5883\u53d8\u91cf\u4f1a\u5f71\u54cd\u6574\u4e2a\u5e73\u53f0\u4e0b\u7684\u6240\u6709\u5e94\u6709\u7a0b\u5e8f<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>System.getenv()\u548cSystem.getProperty()\u662fJava\u79cd\u5e38\u7528\u7684\u4e24\u79cd\u8bfb\u53d6\u73af\u5883\u914d\u7f6e\u4fe1\u606f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10836"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10836"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10836\/revisions"}],"predecessor-version":[{"id":10843,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10836\/revisions\/10843"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}