這里面我們對java中的Runtime類做一個簡單的了解介紹。若不常想到無常和死,雖有絕頂的聰明,照理說也和呆子一樣。
Runtimeo類的使用
一、得到系統內存的一些信息
@Test public void runtimeInfo() { Runtime runtime = Runtime.getRuntime(); int processors = runtime.availableProcessors(); long freeMemory = runtime.freeMemory(); long maxMemory = runtime.maxMemory(); long totalMemory = runtime.totalMemory(); // processors=4, freeMemory=165713400, maxMemory=2837446656, totalMemory=192937984 logger.debug("processors={}, freeMemory={}, maxMemory={}, totalMemory={}", processors, freeMemory, maxMemory, totalMemory); }
二、得到系統的環境變量
@Test public void dirRuntimeProcess() throws IOException, InterruptedException { Process process = Runtime.getRuntime().exec("cmd.exe /c echo %JAVA_HOME%"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String string = null; while ((string = bufferedReader.readLine()) != null) { System.out.println(string); // D:\Java\jdk\jdk1.8.0_152 } process.waitFor(); System.out.println("return: " + process.exitValue()); // return: 0 }
三、得到java的版本號,這個和上述的不一樣
@Test public void getJavaVersion() { try { Process process = Runtime.getRuntime().exec("javac -version"); BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream())); String line = null; while ((line = br.readLine()) != null) System.out.println(line); // javac 1.8.0_152 process.waitFor(); System.out.println("Process exitValue: " + process.exitValue()); } catch (Throwable t) { t.printStackTrace(); } }
四、執行外部命令得到的結果
@Test public void execProgramC() { try { Process process = Runtime.getRuntime().exec("C:/Users/76801/Desktop/huhx.exe"); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; while ((line = br.readLine()) != null) System.out.println(line); // Hello World. process.waitFor(); System.out.println("Process exitValue: " + process.exitValue()); } catch (Throwable t) { t.printStackTrace(); } }
huhx.c比較簡單,就是打印一句話。
#include<stdio.h> void main() { printf("Hello World."); }
五、使用Runtime類導出mysql腳本
@Test public void execMysqldump() throws IOException, InterruptedException { String execCommand = "cmd c/ D:/Java/mysqldump.exe -uhuhx -phuhx boot_learn > D:/bootlearn.sql"; System.out.println("exec command: " + execCommand); Runtime runtime = Runtime.getRuntime(); Process p = runtime.exec(execCommand); StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "Error"); StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "Output"); errorGobbler.start(); outputGobbler.start(); p.waitFor(); System.out.println("successful." + p.exitValue()); }
上述也使用到了網上所說的讀出窗口的標准輸出緩沖區中的內容,仍舊沒有解決Process的waitFor阻塞問題。下面是清空緩沖區的線程代碼:
public class StreamGobbler extends Thread { InputStream is; String type; public StreamGobbler(InputStream is, String type) { this.is = is; this.type = type; } public void run() { try (InputStreamReader isr = new InputStreamReader(is);) { BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { if (type.equals("Error")) { System.out.println("Error :" + line); } else { System.out.println("Debug:" + line); } } } catch (IOException e) { e.printStackTrace(); } } }
代碼的目標是導出mysql數據庫的腳本。沒有找到問題的解決方案,運行環境是win10,jdk1.8。
友情鏈接