shell腳本java程序
java-cp"./classes:./classlib"-Dparam1=zzzzztest.myclass$0
-cp指定classpath
-D指定一個參數,程序內用System.getProperty("param1")訪問
$0把外部調用的參數傳遞給javaclass
Ⅱ 如何在java中執行shell腳本
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException,InterruptedException {
//Runtime.getRuntime().exec("/Users/lijialiang/codetest/stop.sh jmeter");
//Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","/codetest/stop.sh jmeter"});
//Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ps -ef | grep jmeter | grep -v 'grep' |awk '{print $2}'>>result.txt"});
Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ps -ef | grep <span style="font-family: Arial, Helvetica, sans-serif;">jmeter</span><span style="font-family: Arial, Helvetica, sans-serif;"> | awk '{print $2}' | xargs kill -9"});</span>
//Thread.sleep(100000);
}
}
Ⅲ 用shell怎麼調用java程序
你是在linux環境下運行吧!
如果你是要編譯執行單個java文件,當然用javac,和java,如果是在一個shell腳本中妄圖調用java程序,假如這個程序是個jar包,也許可以這么干:在你能保證的路徑下放入可執行的jar包,然後在腳本中寫到:java -jar jar包路徑+jar包名稱.jar,不信可以在終端使用該命令!
------最後,希望採納!畢竟我們純手打!
Ⅳ 如何在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();
Ⅵ shell腳本調JAVA程序,獲取JAVA程序返回值並echo輸出
先運行java程序,然後用echo $?獲取程序的運行結果,如果是字元串,可以使用
result=$(java -jar xxx.jar)來獲取,當然這只是示例,$()中的命令使可以隨便修改的
Ⅶ 如何在linux系統下編寫shell腳本來運行一個javaprogram,,求大神!!
在linux 下編寫shell 腳本運行 java程序和在windows的命令行運行程序是一樣的。
命令行運行java程序的寫法:
編譯:javac xxx.java
執行:java xxx
在linux下相同:
1. 先vi一個shell腳本文件: vi executejava.sh
2. 文件的內容就把剛才的兩句話寫進去就ok了。
3. 執行的話直接:sh excecutejava.sh