package com.chinasoft;
import java.io.IOException;
public class LiunxEXEjava {
public static void main(String[] args) {
Process process=null;
try {
//path 程序路徑
//exec(shell 命令)
process= Runtime.getRuntime().exec("chmod 777 path");
process.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
process這個類是一個抽象類,封裝了一個進程(你在調用linux的命令或者shell腳本就是為了執行一個在linux下執行的程序,所以應該使用process類)。
process類提供了執行從進程輸入,執行輸出到進程,等待進程完成,檢查進程的推出狀態,以及shut down掉進程。
至於詳細的process類的介紹放在以後介紹。
另外還要注意一個類:Runtime類,Runtime類是一個與JVM運行時環境有關的類,這個類是Singleton的。
這里用到的Runtime.getRuntime()方法是取得當前JVM的運行環境,也是java中唯一可以得到運行環境的方法。(另外,Runtime的大部分方法都是實例方法,也就是說每次運行調用的時候都需要調用到getRuntime方法)
下面說說Runtime的exec()方法,這里要注意的有一點,就是public Process exec(String [] cmdArray, String [] envp);這個方法中cmdArray是一個執行的命令和參數的字元串數組,數組的第一個元素是要執行的命令往後依次都是命令的參數,envp感覺應該和C中的execve中的環境變數是一樣的,envp中使用的是name=value的方式。
❷ 如何在java中執行shell腳本
// 用法:Runtime.getRuntime().exec("命令");
String shpath="/test/test.sh"; //程序路徑
Process process =null;
String command1 = 「chmod 777 」 + shpath;
try {
Runtime.getRuntime().exec(command1 ).waitFor();
} catch (IOException e1) {
e1.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
String var="201102"; /參數
String command2 = 「/<a href="https://www..com/s?wd=bin&tn=44039180_cpr&fenlei=-bIi4WUvYETgN-" target="_blank" class="-highlight">bin</a>/sh 」 + shpath + 」 」 + var;
Runtime.getRuntime().exec(command2).waitFor();
❸ java怎麼調用shell腳本
//用法:Runtime.getRuntime().exec("命令");Stringshpath="/test/test.sh";//程序路徑Processprocess=null;Stringcommand1=「chmod777」+shpath;try{Runtime.getRuntime().exec(command1).waitFor();}catch(IOExceptione1){e1.printStackTrace();}catch(InterruptedExceptione){e.printStackTrace();}Stringvar="201102";/參數Stringcommand2=「/bin/sh」+shpath+」」+var;Runtime.getRuntime().exec(command2).waitFor();
❹ 請教個java調用shell命令操作
近日項目中有這樣一個需求:系統中的外幣資金調度完成以後,要將調度信息生成一個Txt文件,然後將這個Txt文件發送到另外一個系統(Kondor)中。生成文件自然使用OutputStreamWirter了,發送文件有兩種方式,一種是用寫個一個類似於FTP功能的程序,另外一種就是使用Java來調用Shell,在Shell中完成文件的發送操作。我們選擇後一種,即當完成外幣資金的調度工作後,用Java的OutputStreamWriter來生成一個Txt文件,然後用Java來調用Shell腳本,在Shell腳本中完成FTP文件到Kondor系統的工作。
以下為Java程序JavaShellUtil.java:import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaShellUtil {
//基本路徑
private static final String basePath = "/tmp/";
//記錄Shell執行狀況的日誌文件的位置(絕對路徑)
private static final String executeShellLogFile = basePath + "executeShell.log";
//發送文件到Kondor系統的Shell的文件名(絕對路徑)
private static final String sendKondorShellName = basePath + "sendKondorFile.sh";
public int executeShell(String shellCommand) throws IOException {
int success = 0;
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferedReader = null;
//格式化日期時間,記錄日誌時使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");
try {
stringBuffer.append(dateFormat.format(new Date())).append("准備執行Shell命令 ").append(shellCommand).append(" \r\n");
Process pid = null;
String[] cmd = {"/bin/sh", "-c", shellCommand};
//執行Shell命令
pid = Runtime.getRuntime().exec(cmd);
if (pid != null) {
stringBuffer.append("進程號:").append(pid.toString()).append("\r\n");
//bufferedReader用於讀取Shell的輸出內容 bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);
pid.waitFor();
} else {
stringBuffer.append("沒有pid\r\n");
}
stringBuffer.append(dateFormat.format(new Date())).append("Shell命令執行完畢\r\n執行結果為:\r\n");
String line = null;
//讀取Shell的輸出內容,並添加到stringBuffer中
while (bufferedReader != null &
&
(line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\r\n");
}
} catch (Exception ioe) {
stringBuffer.append("執行Shell命令時發生異常:\r\n").append(ioe.getMessage()).append("\r\n");
} finally {
if (bufferedReader != null) {
OutputStreamWriter outputStreamWriter = null;
try {
bufferedReader.close();
//將Shell的執行情況輸出到日誌文件中
OutputStream outputStream = new FileOutputStream(executeShellLogFile);
outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
outputStreamWriter.write(stringBuffer.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStreamWriter.close();
}
}
success = 1;
}
return success;
}
}
以下是Shell腳本sendKondorFile.sh,該Shell腳本的作用是FTP文件到指定的位置:
#!/bin/sh
#日誌文件的位置
logFile="/opt/fms2_kondor/sendKondorFile.log"
#Kondor系統的IP地址,會將生成的文件發送到這個地址
kondor_ip=192.168.1.200
#FTP用戶名
ftp_username=kondor
#FTP密碼
ftp_password=kondor
#要發送的文件的絕對路徑
filePath=""
#要發送的文件的文件名
fileName=""
#如果Shell命令帶有參數,則將第一個參數賦給filePath,將第二個參數賦給fileName
if [ $# -ge "1" ]
then
filePath=$1
else
echo "沒有文件路徑"
echo "沒有文件路徑\n" >
>
$logFile
return
fi
if [ $# -ge "2" ]
then
fileName=$2
else
echo "沒有文件名"
echo "沒有文件名\n" >
>
$logFile
return
fi
echo "要發送的文件是 ${filePath}/${fileName}"
cd ${filePath}
ls $fileName
if (test $? -eq 0)
then
echo "准備發送文件:${filePath}/${fileName}"
else
echo "文件 ${filePath}/${fileName} 不存在"
echo "文件 ${filePath}/${fileName} 不存在\n" >
>
$logFile
return
fi
ftp -n ${kondor_ip} <
<
_end
user ${ftp_username} ${ftp_password}
asc
prompt
put $fileName
bye
_end
echo "`date +%Y-%m-%d' '%H:%M:%S` 發送了文件 ${filePath}/${fileName}"
echo "`date +%Y-%m-%d' '%H:%M:%S` 發送了文件 ${filePath}/${fileName}\n" >
>
$logFile
調用方法為:
JavaShellUtil javaShellUtil = new JavaShellUtil();
//參數為要執行的Shell命令,即通過調用Shell腳本sendKondorFile.sh將/temp目錄下的tmp.pdf文件發送到192.168.1.200上
int success = javaShellUtil.executeShell("sh /tmp/sendKondorFile.sh /temp tmp.pdf");
❺ 如何在java中執行shell腳本
過CommandHelper.execute方法可以執行命令,該類實現
復制代碼代碼如下:
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對象輸出結果錯誤信息。該類實現
復制代碼代碼如下:
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;
}
}
❻ java怎麼執行shell腳本
如果shell腳本和java程序運行在不同的伺服器上,可以使用遠程執行Linux命令執行包,使用ssh2協議連接遠程伺服器,並發送執行命令就行了,ganymed.ssh2相關mave配置如下,你可以自己網路搜索相關資料。
如果shell腳本和java程序在同一台伺服器上,
這里不得不提到java的process類了。
process這個類是一個抽象類,封裝了一個進程(你在調用linux的命令或者shell腳本就是為了執行一個在linux下執行的程序,所以應該使用process類)。
process類提供了執行從進程輸入,執行輸出到進程,等待進程完成,檢查進程的推出狀態,以及shut down掉進程。
<dependency>
<groupId>com.ganymed.ssh2</groupId>
<artifactId>ganymed-ssh2-build</artifactId>
<version>210</version>
</dependency>
本地執行命令代碼如下:
Stringshpath="/test/test.sh";//程序路徑
Processprocess=null;
Stringcommand1=「chmod777」+shpath;
process=Runtime.getRuntime().exec(command1);
process.waitFor();
❼ 如何用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 調用 shell 腳本
在寫程序時,有時需要在java程序中調用shell腳本,可以通過Runtime的exec方法來調用shell程序,運行腳本。每個Java 應用程序都有一個Runtime 類實例,使應用程序能夠與其運行的環境相連接。通過Runtime對象可以返回運行環境的情況,包括CPU數,虛擬機內存大小等,並能夠通過exec方法調用執行命令。可以通過getRuntime 方法獲取當前Runtime實例。 public boolean ExeShell(){ Runtime rt = Runtime.getRuntime(); try { Process p = rt.exec(checkShellName); if(p.waitFor() != 0) return false; } catch (IOException e) { SysLog.error("沒有找到檢測腳本"); return false; } catch (InterruptedException e) { e.printStackTrace(); return false; } return true; } 其中p.waitFor()語句用來等待子進程結束,其返回值為進程結束退出碼。
❾ 如何在java中執行shell腳本
在java中執行shell腳本 用法:Runtime.getRuntime().exec("命令");
String shpath="/test/test.sh"; //程序路徑
Process process =null;
String command1 = 「chmod 777 」 + shpath;
try {
Runtime.getRuntime().exec(command1 ).waitFor();
} catch (IOException e1) {
e1.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
String var="201102"; /參數
String command2 = 「/bin/sh 」 + shpath + 」 」 + var;
Runtime.getRuntime().exec(command2).waitFor();
❿ 怎麼通過java去調用並執行shell腳本以及問題總結
對於第一個問題:java抓取,並且把結果打包。那麼比較直接的做法就是,java接收各種消息(db,metaq等等),然後藉助於jstorm集群進行調度和抓取。
最後把抓取的結果保存到一個文件中,並且通過調用shell打包, 回傳。 也許有同學會問,
為什麼不直接把java調用odps直接保存文件,答案是,我們的集群不是hz集群,直接上傳odps速度很有問題,因此先打包比較合適。(這里不糾結設計了,我們回到正題)
java調用shell的方法
通過ProcessBuilder進行調度
這種方法比較直觀,而且參數的設置也比較方便, 比如我在實踐中的代碼(我隱藏了部分業務代碼):
ProcessBuilderpb = new ProcessBuilder("./" + RUNNING_SHELL_FILE, param1,
param2, param3);
pb.directory(new File(SHELL_FILE_DIR));
int runningStatus = 0;
String s = null;
try {
Process p = pb.start();
try {
runningStatus = p.waitFor();
} catch (InterruptedException e) {
}
} catch (IOException e) {
}
if (runningStatus != 0) {
}
return;
這里有必要解釋一下幾個參數:
RUNNING_SHELL_FILE:要運行的腳本
SHELL_FILE_DIR:要運行的腳本所在的目錄; 當然你也可以把要運行的腳本寫成全路徑。
runningStatus:運行狀態,0標識正常。 詳細可以看java文檔。
param1, param2, param3:可以在RUNNING_SHELL_FILE腳本中直接通過1,2,$3分別拿到的參數。
直接通過系統Runtime執行shell
這個方法比較暴力,也比較常用, 代碼如下:
p = Runtime.getRuntime().exec(SHELL_FILE_DIR + RUNNING_SHELL_FILE + " "+param1+" "+param2+" "+param3);
p.waitFor();
我們發現,通過Runtime的方式並沒有builder那麼方便,特別是參數方面,必須自己加空格分開,因為exec會把整個字元串作為shell運行。
可能存在的問題以及解決方法
如果你覺得通過上面就能滿足你的需求,那麼可能是要碰壁了。你會遇到以下情況。
沒許可權運行
這個情況我們團隊的朱東方就遇到了, 在做DTS遷移的過程中,要執行包裡面的shell腳本, 解壓出來了之後,發現執行不了。 那麼就按照上面的方法授權吧
java進行一直等待shell返回
這個問題估計更加經常遇到。 原因是, shell腳本中有echo或者print輸出, 導致緩沖區被用完了! 為了避免這種情況, 一定要把緩沖區讀一下, 好處就是,可以對shell的具體運行狀態進行log出來。 比如上面我的例子中我會變成:
ProcessBuilderpb = new ProcessBuilder("./" + RUNNING_SHELL_FILE, keyword.trim(),
taskId.toString(), fileName);
pb.directory(new File(CASPERJS_FILE_DIR));
int runningStatus = 0;
String s = null;
try {
Process p = pb.start();
BufferedReaderstdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReaderstdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) {
LOG.error(s);
}
while ((s = stdError.readLine()) != null) {
LOG.error(s);
}
try {
runningStatus = p.waitFor();
} catch (InterruptedException e) {
}
記得在start()之後, waitFor()之前把緩沖區讀出來打log, 就可以看到你的shell為什麼會沒有按照預期運行。 這個還有一個好處是,可以讀shell裡面輸出的結果, 方便java代碼進一步操作。
也許你還會遇到這個問題,明明手工可以運行的命令,java調用的shell中某一些命令居然不能執行,報錯:命令不存在!
比如我在使用casperjs的時候,手工去執行shell明明是可以執行的,但是java調用的時候,發現總是出錯。
通過讀取緩沖區就能發現錯誤日誌了。 我發現即便自己把安裝的casperjs的bin已經加入了path中(/etc/profile,
各種bashrc中)還不夠。 比如:
exportNODE_HOME="/home/admin/node"
exportCASPERJS_HOME="/home/admin/casperjs"
exportPHANTOMJS_HOME="/home/admin/phantomjs"
exportPATH=$PATH:$JAVA_HOME/bin:/root/bin:$NODE_HOME/bin:$CASPERJS_HOME/bin:$PHANTOMJS_HOME/bin
原來是因為java在調用shell的時候,默認用的是系統的/bin/下的指令。特別是你用root許可權運行的時候。 這時候,你要在/bin下加軟鏈了。針對我上面的例子,就要在/bin下加軟鏈:
ln -s /home/admin/casperjs/bin/casperjscasperjs;
ln -s /home/admin/node/bin/nodenode;
ln -s /home/admin/phantomjs/bin/phantomjsphantomjs;
這樣,問題就可以解決了。
如果是通過java調用shell進行打包,那麼要注意路徑的問題了
因為shell裡面tar的壓縮和解壓可不能直接寫:
tar -zcf /home/admin/data/result.tar.gz /home/admin/data/result
直接給你報錯,因為tar的壓縮源必須到路徑下面, 因此可以寫成
tar -zcf /home/admin/data/result.tar.gz -C /home/admin/data/ result
如果我的shell是在jar包中怎麼辦?
答案是:解壓出來。再按照上面指示進行操作。(1)找到路徑
String jarPath = findClassJarPath(ClassLoaderUtil.class);
JarFiletopLevelJarFile = null;
try {
topLevelJarFile = new JarFile(jarPath);
Enumeration<JarEntry> entries = topLevelJarFile.entries();
while (entries.hasMoreElements()) {
JarEntryentry = entries.nextElement();
if (!entry.isDirectory() entry.getName().endsWith(".sh")) {
對你的shell文件進行處理
}
}
對文件處理的方法就簡單了,直接touch一個臨時文件,然後把數據流寫入,代碼:
FileUtils.touch(tempjline);
tempjline.deleteOnExit();
FileOutputStreamfos = new FileOutputStream(tempjline);
IOUtils.(ClassLoaderUtil.class.getResourceAsStream(r), fos);
fos.close();