導航:首頁 > 編程語言 > 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命令相關的資料

熱點內容
qq音樂不能播放 瀏覽:431
15g的文件 瀏覽:456
Win10系統無法在消息文件中為 瀏覽:635
ccd文件 瀏覽:826
win7查看文件啟動路徑 瀏覽:693
溧水114網站電話多少 瀏覽:687
蘋果表盤文件下好了怎麼安裝 瀏覽:692
社工庫都有哪些網站 瀏覽:886
恢復刪除的word文件 瀏覽:822
盧本偉什麼事情在網路上走紅 瀏覽:522
sp文件夾在裡面哪個是卸載程序 瀏覽:70
英雄聯盟新版本611 瀏覽:483
三星安卓70夜間模式 瀏覽:135
linux文件怎麼解除佔用 瀏覽:743
app爆紅怎麼處理 瀏覽:728
為什麼沒有辦法復制文件到光碟 瀏覽:620
小孩學專注力和編程哪個好 瀏覽:43
word中八卦 瀏覽:310
javaee上傳整個文件夾 瀏覽:671
pdf文件修改器下載 瀏覽:664

友情鏈接