一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java運(yùn)行shell腳本方法示例

java運(yùn)行shell腳本方法示例

2019-10-24 16:42java教程網(wǎng) Java教程

利用Runtime.execute方法,我們可以在Java程序中運(yùn)行Linux的Shell腳本,或者執(zhí)行其他程序

現(xiàn)在通過CommandHelper.execute方法可以執(zhí)行命令,該類實(shí)現(xiàn)

 

復(fù)制代碼代碼如下:

package javaapplication3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 *
 * @author chenshu
 */
public class CommandHelper {
    //default time out, in millseconds
    public static int DEFAULT_TIMEOUT;
    public static final int DEFAULT_INTERVAL = 1000;
    public static long START;
    public static CommandResult exec(String command) throws IOException, InterruptedException {
        Process process = Runtime.getRuntime().exec(command);
        CommandResult commandResult = wait(process);
        if (process != null) {
process.destroy();
        }
        return commandResult;
    }
    private static boolean isOverTime() {
        return System.currentTimeMillis() - START >= DEFAULT_TIMEOUT;
    }
    private static CommandResult wait(Process process) throws InterruptedException, IOException {
        BufferedReader errorStreamReader = null;
        BufferedReader inputStreamReader = null;
        try {
errorStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
inputStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//timeout control
START = System.currentTimeMillis();
boolean isFinished = false;
for (;;) {
if (isOverTime()) {
CommandResult result = new CommandResult();
result.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);
result.setOutput("Command process timeout");
return result;
}
if (isFinished) {
CommandResult result = new CommandResult();
result.setExitValue(process.waitFor());
//parse error info
if (errorStreamReader.ready()) {
StringBuilder buffer = new StringBuilder();
String line;
while ((line = errorStreamReader.readLine()) != null) {
buffer.append(line);
}
result.setError(buffer.toString());
}
//parse info
if (inputStreamReader.ready()) {
StringBuilder buffer = new StringBuilder();
String line;
while ((line = inputStreamReader.readLine()) != null) {
buffer.append(line);
}
result.setOutput(buffer.toString());
}
return result;
}
try {
isFinished = true;
process.exitValue();
} catch (IllegalThreadStateException e) {
// process hasn't finished yet
isFinished = false;
Thread.sleep(DEFAULT_INTERVAL);
}
}
        } finally {
if (errorStreamReader != null) {
try {
errorStreamReader.close();
} catch (IOException e) {
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
}
}
        }
    }
}

 

CommandHelper類使用了CommandResult對(duì)象輸出結(jié)果錯(cuò)誤信息。該類實(shí)現(xiàn)
 

復(fù)制代碼代碼如下:

 

package javaapplication3;
/**
 *
 * @author chenshu
 */
public class CommandResult {
    public static final int EXIT_VALUE_TIMEOUT=-1;
    private String output;
    void setOutput(String error) {
        output=error;
    }
    String getOutput(){
        return output;
    }
    int exitValue;
    void setExitValue(int value) {
        exitValue=value;
    }
    int getExitValue(){
        return exitValue;
    }
    private String error;
    /**
     * @return the error
     */
    public String getError() {
        return error;
    }
    /**
     * @param error the error to set
     */
    public void setError(String error) {
        this.error = error;
    }
}

 

現(xiàn)在看看調(diào)用代碼的演示(main函數(shù)接受一個(gè)超時(shí)參數(shù)):

 

復(fù)制代碼代碼如下:

public static void main(String[] args) {
        try {
int timeout = Integer.parseInt(args[0]);
CommandHelper.DEFAULT_TIMEOUT = timeout;
CommandResult result = CommandHelper.exec("mkdir testdir");
if (result != null) {
System.out.println("Output:" + result.getOutput());
System.out.println("Error:" + result.getError());
}
        } catch (IOException ex) {
System.out.println("IOException:" + ex.getLocalizedMessage());
        } catch (InterruptedException ex) {
System.out.println("InterruptedException:" + ex.getLocalizedMessage());
        }
    }

 

結(jié)果會(huì)創(chuàng)建一個(gè)testdir目錄。
我嘗試用這種方法創(chuàng)建通過ssh登錄到遠(yuǎn)程機(jī)器,遇到兩個(gè)問題:
1)如果希望沒有人機(jī)對(duì)話方式,則需要使用命令sshpass -p password ssh user@targetIP 'command'
2) 在NetBeans上直接運(yùn)行工程是不行的,因?yàn)闄?quán)限不夠,需要在終端里運(yùn)行java javaapplication3.Main
3) 很多命令不能運(yùn)行,只有如pwd等命令可以運(yùn)行,原因還不清楚,最好改用Ganymed SSH-2庫(kù)或者其他類似Java庫(kù),我會(huì)在下一篇文章中介紹如何使用。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲日本视频在线 | 99精彩视频在线观看 | 亚洲精品AV无码永久无码 | 91亚洲精品第一综合不卡播放 | jm漫天堂破解版 | 国产成人综合精品 | 日本妇人成熟免费不卡片 | 欧美激情亚洲 | 色哟哟哟在线精品观看视频 | 国产一级特黄aa大片免费 | 俄罗斯激情性孕妇孕交大全 | 四虎影视4hu最新地址在线884 | 好紧好爽再叫浪一点点潘金莲 | 国产综合欧美日韩视频一区 | 日本成年片高清在线观看 | 成人资源在线观看 | 美女下面揉出水免费视频 | 1769在线观看 | 午夜福利08550| 国产精品自在欧美一区 | 国亚洲欧美日韩精品 | 国产成人a∨麻豆精品 | 32pao强力打造免费高速高 | 精品久久久久久国产91 | 猛吸奶水的老汉 | 国产一卡2卡3卡四卡国色天香 | 免费视频精品一区二区三区 | 日本高清免费观看 | 日本午夜视频 | 免费观看在线永久免费xx视频 | 成人精品福利 | 亚洲精品91香蕉综合区 | 国产免费丝袜调教视频 | 国产极品美女在线 | 91.prom在线观看国产 | 亚洲国产在 | 免费观看成年肉动漫网站 | 亚洲视频免费在线观看 | 久久国产伦子伦精品 | 亚洲精品福利在线 | 精品手机在线视频 |