當前位置:首頁 » 編程軟體 » shell腳本java程序

shell腳本java程序

發布時間: 2022-12-07 05:16:12

Ⅰ 使用java怎麼寫一個shell腳本

  1. java-cp"./classes:./classlib"-Dparam1=zzzzztest.myclass$0

  2. -cp指定classpath

  3. -D指定一個參數,程序內用System.getProperty("param1")訪問

  4. $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

熱點內容
外文翻譯android 發布:2024-04-26 19:03:30 瀏覽:89
土金木配置怎麼樣 發布:2024-04-26 18:52:50 瀏覽:610
這台電腦如何訪問另一台電腦伺服器 發布:2024-04-26 18:51:08 瀏覽:627
怎麼快速了解電腦的配置 發布:2024-04-26 18:42:11 瀏覽:997
rsa加密演算法例子 發布:2024-04-26 18:40:29 瀏覽:243
thinkphp緩存關閉 發布:2024-04-26 18:19:32 瀏覽:96
linux信號捕捉 發布:2024-04-26 18:19:27 瀏覽:934
編譯有哪兩種模式 發布:2024-04-26 17:53:30 瀏覽:871
伺服器電腦上能用嗎 發布:2024-04-26 17:44:42 瀏覽:560
組件式編程 發布:2024-04-26 17:19:57 瀏覽:943