如果需要外部传入参数,可以传入args或者使用 读取配置文件的方式
public class Execute {
public static void main(String[] args) {
scan();
}
public static void scan(){
//这段代码为了打成jar包的时候能用=======================
ResourceLoader resourceLoader=new DefaultResourceLoader();
Resource resource=resourceLoader.getResource("classpath:s.exe");
InputStream is=null;
try {
is=resource.getInputStream();
FileOutputStream fos = new FileOutputStream("/D:/tmp/s.exe");
byte[] b = new byte[1024];
int length;
while((length=is.read(b))>0){
fos.write(b,0,length);
}
is.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
//================================================================
Runtime runtime=Runtime.getRuntime();
BufferedReader stdInput=null;
BufferedReader stdError=null;
try {
Process process = runtime.exec("/D:/tmp/s.exe TCP 192.168.0.1 192.168.0.254 22 50");
//获取正常输出流
stdInput= new BufferedReader(
new InputStreamReader(process.getInputStream()));
//获取错误输出流
stdError = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
System.out.println("output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (stdInput!=null){
try {
stdInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (stdError!=null){
try {
stdError.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}