日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java基础----Runtime类的使用(一)

發(fā)布時間:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java基础----Runtime类的使用(一) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  這里面我們對java中的Runtime類做一個簡單的了解介紹。若不常想到無常和死,雖有絕頂?shù)穆斆?#xff0c;照理說也和呆子一樣。

?

Runtimeo類的使用

一、得到系統(tǒng)內(nèi)存的一些信息

@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=192937984logger.debug("processors={}, freeMemory={}, maxMemory={}, totalMemory={}", processors, freeMemory, maxMemory, totalMemory); }

?

二、得到系統(tǒng)的環(huán)境變量

@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 }

?

三、得到j(luò)ava的版本號,這個和上述的不一樣

@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();} }

?

四、執(zhí)行外部命令得到的結(jié)果

@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類導(dǎo)出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()); }

上述也使用到了網(wǎng)上所說的讀出窗口的標(biāo)準(zhǔn)輸出緩沖區(qū)中的內(nèi)容,仍舊沒有解決Process的waitFor阻塞問題。下面是清空緩沖區(qū)的線程代碼:

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();}} }

?代碼的目標(biāo)是導(dǎo)出mysql數(shù)據(jù)庫的腳本。沒有找到問題的解決方案,運(yùn)行環(huán)境是win10,jdk1.8。

?

友情鏈接

?

轉(zhuǎn)載于:https://www.cnblogs.com/huhx/p/baseusejavaruntime1.html

總結(jié)

以上是生活随笔為你收集整理的java基础----Runtime类的使用(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。