当前位置:首页 » 编程软件 » shell脚本运行java

shell脚本运行java

发布时间: 2022-09-20 03:09:56

Ⅰ 如何在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);
}
}

Ⅱ 如何在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();

Ⅲ 怎么在shell脚本中运行java代码

// 用法: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脚本

过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脚本

在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脚本

使用shell脚本启动zookeeper步骤:采用shell脚本启动zookeeper,首先新建文件start.sh写入内容(rh1rh2rh3分别是主机名。此处需要ssh):#!/bin/shecho“startzkServer…”foriinrh1rh2rh3dossh$i“/usr/local/zookeeper3.4/bin/zkServer.shstart”done写好后保存,加上执行权限:chmo+xstart.sh运行:./start.sh看见启动成功了,有输出。但是输入jps查看的时候,会发现没有QuorumPeerMain进程。说明没有启动成功。分析原因首先知道交互式shell和非交互式shell、登录shell和非登录shell是有区别的在登录shell里,环境信息需要读取/etc/profile和~/.bash_profile,~/.bash_login,and~/.profile按顺序最先的一个,并执行其中的命令。除非被—noprofile选项禁止了;在非登录shell里,环境信息只读取/etc/bash.bashrc和~/.bashrc手工执行是属于登陆shell,脚本执行数据非登陆shell,而我的linux环境配置中只对/etc/profile进行了jdk1.6等环境的配置,所以脚本执行/usr/local/zookeeper3.4/bin/zkServer.shstart启动zookeeper失败了解决方法把profile的配置信息echo到.bashrc中echo‘source/etc/profile’~/.bashrc在/zookeeper/bin/zkEnv.sh的中开始位置添加exportJAVA_HOME=/usr/local/jdk1.6(就像hadoop中对hadoop-env.sh的配置一样)采用shell脚本启动zookeeper,首先新建文件start.sh写入内容(rh1rh2rh3分别是主机名。此处需要ssh):#!/bin/shecho“startzkServer就可以了。

Ⅶ linux shell脚本如何启动一个java进程

在shell里面直接调用即可。
    1,编译一个java文件为a.class。
    2,编写shell脚本b
.sh

#!/bin/bash
java a.class    3,放在同一目录下运行shell即可。如果要后台运行,bash
b.sh&

Ⅷ 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();

Ⅸ 如何在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

Ⅹ 如何在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=-uH-Wn1nsPHFbmyP--bIi4WUvYETgN-" target="_blank" class="-highlight">bin</a>/sh ” + shpath + ” ” + var;
Runtime.getRuntime().exec(command2).waitFor();

热点内容
scratch少儿编程课程 发布:2025-04-16 17:11:44 浏览:624
荣耀x10从哪里设置密码 发布:2025-04-16 17:11:43 浏览:353
java从入门到精通视频 发布:2025-04-16 17:11:43 浏览:68
php微信接口教程 发布:2025-04-16 17:07:30 浏览:293
android实现阴影 发布:2025-04-16 16:50:08 浏览:786
粉笔直播课缓存 发布:2025-04-16 16:31:21 浏览:336
机顶盒都有什么配置 发布:2025-04-16 16:24:37 浏览:201
编写手游反编译都需要学习什么 发布:2025-04-16 16:19:36 浏览:795
proteus编译文件位置 发布:2025-04-16 16:18:44 浏览:353
土压缩的本质 发布:2025-04-16 16:13:21 浏览:581