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