導航:首頁 > 編程語言 > java怎麼調用linux命令

java怎麼調用linux命令

發布時間:2023-09-18 19:47:04

java中如何執行linux命令

執行linux命令基,基本思路是從控制台獲得輸入的指令,啟動命令行執行命令,捕捉異常,示例如下:

publicclassTestRunTime{

publicstaticvoidmain(String[]args)throwsIOException,InterruptedException{
Stringcmd="";

if(args==null||args.length==0){
System.out.println("請輸入命令行參數");
}else{

for(inti=0;i<args.length;i++){//獲得輸入的命令
cmd+=args[i]+"";
}
}


try{
Processprocess=Runtime.getRuntime().exec(cmd);//執行命令

InputStreamReaderir=newInputStreamReader(process.getInputStream());
LineNumberReaderinput=newLineNumberReader(ir);

Stringline;
while((line=input.readLine())!=null){//輸出結果
System.out.println(line);
}
}catch(java.io.IOExceptione){
System.err.println("IOException"+e.getMessage());//捕捉異常
}
}
}

㈡ 怎麼用java代碼運行linux命令

以下方法支持Linux和windows兩個系統的命令行調用。還用到了apache的lang工具包commons-lang3-3.1.jar來判斷操作系統類型、也用到了和log4j-1.2.16.jar來列印日誌。至於rm -rf 是否能成功刪除文件,可以手動去調用命令行試試。

privateStringcallCmd(Stringcmd)throwsInterruptedException,UnHandledOSException,ExecuteException{
if(SystemUtils.IS_OS_LINUX){
try{
//使用Runtime來執行command,生成Process對象
Processprocess=Runtime.getRuntime().exec(
newString[]{"/bin/sh","-c",cmd});
intexitCode=process.waitFor();
//取得命令結果的輸出流
InputStreamis=process.getInputStream();
//用一個讀輸出流類去讀
InputStreamReaderisr=newInputStreamReader(is);
//用緩沖器讀行
BufferedReaderbr=newBufferedReader(isr);
Stringline=null;
StringBuildersb=newStringBuilder();
while((line=br.readLine())!=null){
System.out.println(line);
sb.append(line);
}
is.close();
isr.close();
br.close();
returnsb.toString();
}catch(java.lang.NullPointerExceptione){
System.err.println("NullPointerException"+e.getMessage());
logger.error(cmd);
}catch(java.io.IOExceptione){
System.err.println("IOException"+e.getMessage());
}
thrownewExecuteException(cmd+"執行出錯!");
}

if(SystemUtils.IS_OS_WINDOWS){
Processprocess;
try{
//process=newProcessBuilder(cmd).start();
String[]param_array=cmd.split("[\s]+");
ProcessBuilderpb=newProcessBuilder(param_array);
process=pb.start();
/*process=Runtime.getRuntime().exec(cmd);*/
intexitCode=process.waitFor();
InputStreamis=process.getInputStream();
InputStreamReaderisr=newInputStreamReader(is);
BufferedReaderbr=newBufferedReader(isr);
Stringline;
StringBuildersb=newStringBuilder();

while((line=br.readLine())!=null){
System.out.println(line);
sb.append(line);
}
is.close();
isr.close();
br.close();
returnsb.toString();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
thrownewExecuteException(cmd+"執行出錯!");
}

thrownewUnHandledOSException("不支持本操作系統");
}

㈢ java程序如何在linux伺服器上運行

直接與JDK在Linux下Java程序開發,你需要三樣東西:1,
文本編輯器,你可以選擇VIM,但考慮到Windows的習慣,建議用gedit
2。編譯器,它是javac的
3解釋器的程序,它是Java

到Ubuntu為例:內容
$ gedit中Hello.java

Hello.java可能因此:
---
公共類你好{
公共靜態無效的主要(字串[] args){
的System.out.println(「HelloWorld」的);
}}

---

$ LS
Hello.java

$的javac Hello.java
....
BR> $ java的你好
的Helloworld

以上,而在windows基本相同。
中國我覺得你的問題可能是如何安裝的JDK。 1.在
很多方法,你可以根據JDK的Linux二進制版本下載到太陽的主頁,然後才能進行
$存取許可權chmod a + X JDK-XXX-xxx.bin

以root許可權運行
#。 / JDK-XXX-xxx.bin

2.如果您使用的是RedHat,FeforaCore的Linux版本是這樣,那麼你可以下載JDK的rpm包,然後在root許可權安裝:
#rpm -ivh JDK -xxx-xxx.rpm

3.如果使用這樣的系統的Debian,Ubuntu的,這是很簡單的,就這樣任:
$命令和apt-get安裝JDK ...
更詳細的內容可以看到Ubuntu的中國維基評論,
寫的很詳細以上只是一個粗略的概述的步驟,當然你不應該因為它不使用Linux,你應該了解

Sygwin不是一個編輯器,而是一個Win32的Linux開發環境下實現的。
您可以在sygwin安裝JDK。
你問具體地講,它不會安裝JDK,或者不使用JDK編譯,或者不知道如何使用文本編輯器?

㈣ 如何用java調用linux shell命令

**
* 運行shell腳本
* @param shell 需要運行的shell腳本
*/
public static void execShell(String shell){
try {
Runtime rt = Runtime.getRuntime();
rt.exec(shell);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 運行shell
*
* @param shStr
* 需要執行的shell
* @return
* @throws IOException
*/
public static List runShell(String shStr) throws Exception {
List<String> strList = new ArrayList();

Process process;
process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null){
strList.add(line);
}

return strList;
}

閱讀全文

與java怎麼調用linux命令相關的資料

熱點內容
頁面自適應屏幕如何調整代碼數據 瀏覽:681
jsjson工具 瀏覽:299
資料庫中如何備份一張表的數據 瀏覽:739
網路設備能用到什麼 瀏覽:64
暴風轉碼如何添加文件夾 瀏覽:515
延安整合網路營銷有哪些 瀏覽:74
查找word打開過的文件在哪裡 瀏覽:137
b樹java代碼 瀏覽:683
電腦文件存儲 瀏覽:657
蘭州中考徵集志願在哪個網站 瀏覽:215
cs文件上傳下載 瀏覽:244
拷貝文件到根目錄下重命名linux 瀏覽:603
api函數的頭文件 瀏覽:249
華為怎麼綁定迷你編程 瀏覽:215
機構怎麼申請少兒編程考級 瀏覽:495
崑山數控編程哪裡好學 瀏覽:459
jspcfor跳出 瀏覽:65
word怎麼插入羅馬數字i 瀏覽:315
哪個網站可以找到法人代表 瀏覽:106
蘋果5s日版a1453支持什麼網路 瀏覽:297

友情鏈接