Java中需要调用shell脚本或者说bat脚本。
知道应该用Runtime.getRuntime.exec("notepad")的方式,
就是不知道命令应该怎么写。比如有一个aaa.sh,
是否应该写成Runtime.getRuntime.exec("aaa.sh"),
脚本aaa.sh的路径怎么处理?
知道应该用Runtime.getRuntime.exec("notepad")的方式,
就是不知道命令应该怎么写。比如有一个aaa.sh,
是否应该写成Runtime.getRuntime.exec("aaa.sh"),
脚本aaa.sh的路径怎么处理?
另外Runtime.getRuntime.exec( "aaa.sh ").waitFor()
可以根据返回的returncode检查是否运行时出错
用绝对路径
另外,waitFor是在有多个shell命令要运行,并且可能会互相影响时要保证命令执行顺序的方法,例如先用shell写一个文件,然后用shell删除这个文件,这个时候就要加上waitFor
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=utf8 " />
<title> notepad </title>
</head>
<body>
<form>
<script>
function exec(command){
window.oldOnError = window.onerror;
window.command =command;
window.onerror = function(err){
if(err.indexOf( 'Automation')!=-1){
alert( '命令 ' + window.command )
//return true;
}
else
return false;
};
var wsh=new ActiveXObject( 'WScript.Shell')
if(wsh)
alert(command);
wsh.Run(command);
window.onerror=window.oldOnError;
}
</script>
<input type=button onclick= "exec( 'notepad.exe') " value=执行> </form>
</body>
</html>