如何在java中执行shell脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在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;

参考技术A // 用法:Runtime.getRuntime().exec("命令"); String shpath="/test/test.sh"; //程序路径 Process process =null; String command1 = “chmod 777 ” + shpath; try Runtime.getRuntime().exec(command1 ).waitFor(); catch (IOException e...

shell脚本中调用JAVA 程序,如何让JAVA运行结束后,再执行下面命令(只有java运行完,下面对数据的处理才有用)

思路,调用JAVA ,运行完后,使用sqlplus 修改数据库状态,根据数据库状态再 nohup sh single1.sh ,现在JAVA 没运行完,就修改了数据库的状态,操作系统是AIX 的
现在大多数提到的方法就是 写个不断检测的JAVA 是否运行完的程序,还有更简单的方法吗

调用java的时候不要挂在后台执行就可以了, 如果说你的java程序名叫 Test.class 或者 Test.jar

shell脚本里调用JAVA程序
#!/bin/sh
java Test.class ##或者java -jar Test.jar
stat=`sqlplus .......`
if [ $stat = "**"]
then
nohup sh single1.sh
fi

shell里的命令也是按顺序执行的,java调用没返回的时候是不会继续往下执行的
如果你调用java程序的时候,命令后面带了个 & 符,则这条命令是在后台执行的,shell会继续执行下一条命令
参考技术A 在你的shell中添加一条检查java程序运行状态的不就好了么,也不对程序整体结构造成多大的破坏。
看你脚本的设置,应该是一个demon进程,本身不就是一个不断检查状态的过程么追问

详细点,怎么添加

以上是关于如何在java中执行shell脚本的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本中调用JAVA 程序,如何让JAVA运行结束后,再执行下面命令(只有java运行完,下面对数据的处理才有用)

如何在shell脚本里调用另一个shell脚本

如何shell脚本中,执行一个其他程序的命令,然后保存这个执行这个命令的结果和过程?

如何在shell脚本中调用另一个shell的结果

如何在Linux中执行Shell脚本?

shell 在脚本中执行了source /etc/profile 不生效