idea向System.getenv()添加系統環境變量的操作
最近在接入阿里云的短信服務,在使用阿里云短信服務的SDK過程中想看看SDK中HttpUtil 中
public static String debugHttpRequest(HttpRequest request) {if (isHttpDebug) { StringBuilder debugString = new StringBuilder(); String sysUrl = request.getSysUrl(); URL url = null; try {url = new URL(sysUrl);debugString.append('> ' + request.getSysMethod() + ' ' + url.getProtocol().toUpperCase() + '/1.1n> ');debugString.append('Host : ' + url.getHost() + 'n> '); } catch (MalformedURLException e) {debugString.append('> ' + request.getSysMethod() + ' ' + sysUrl + 'n> ');debugString.append('Host : ' + sysUrl + 'n> '); } Map<String, String> requestHeaders = request.getSysHeaders(); for (Entry<String, String> entry : requestHeaders.entrySet()) {debugString.append(entry.getKey() + ' : ' + entry.getValue() + 'n> '); } debugString.append('Request URL : ' + sysUrl + 'n> '); if (isHttpContentDebug) {try { debugString.append('n' + request.getHttpContentString());} catch (ClientException e) { debugString.append('n' + 'Can not parse response due to unsupported encoding : ' + request .getSysEncoding());} } log.info('n' + debugString); return debugString.toString();} else { return null;} }
上述方法的debug信息,但是由于isHttpDebug是在靜態代碼塊中通過讀取系統環境變量判斷的
static {Boolean flag = 'sdk'.equalsIgnoreCase(System.getenv('DEBUG'));isHttpDebug = flag;isHttpContentDebug = flag; }
所以來想辦法如何設置這個DEBUG參數
讀取系統環境變量for (String s : System.getenv().keySet()) { System.out.println(s+':'+System.getenv(s));}設置系統環境變量
至此,通過idea設置程序運行系統環境變量就完成了。可以通過System.getenv()來查看設置的系統環境變量。
mac上ide中無法獲取環境變量的問題工作環境:mac
IDE:eclipse or IntelliJ IDEA
工作中需要用環境變量來設置參數,然后在程序啟動時發現之前在.bash_profile中配置的環境變量都讀不到,命令行echo一下是生效的。
后來定位到原因是idea啟動沒有獲取到環境變量。。我之前的啟動方式是直接雙擊圖標。
之后關閉ide,通過bash命令 open /Applications/xxx.app啟動ide。
System.out.println(System.getenv('LOCAL_PROXY'));
獲取到了之前配置的環境變量,問題解決。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: