- 论坛徽章:
- 0
|
紧急询问;java程序中如果调用写好的shell程序,要传递参数
- package test;
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- public class javashell {
- static public void main(String[] args) {
- javashell instance = new javashell();
- instance.callCommand();
- }
- static void callCommand(){
- String[] cmd = { "rsh", "sesun4", "cat /tmp/anyfile.txt " };
- try {
- Process ps = Runtime.getRuntime().exec(cmd);
- System.out.print(loadStream(ps.getInputStream()));
- //System.err.print(loadStream(ps.getErrorStream()));
- }catch(IOException ioe){
- ioe.printStackTrace();
- }
- }
- // read an input-stream into a String
- static String loadStream(InputStream in) throws IOException {
- int ptr = 0;
- int i = 0 ;
- StringBuffer buffer = null ;
- try{
- in = new BufferedInputStream(in);
- buffer = new StringBuffer();
- while( (ptr = in.read()) != -1 ) {
- i ++ ;
- buffer.append((char)ptr);
- //System.out.println(i + " " + (char)ptr);
- if((i%1000)==0)
- System.out.println(i + " " + (char)ptr);
- System.out.println(buffer);
- }
- System.out.println("char set" + i);
- }catch(IOException ioe){
- ioe.printStackTrace();
- }
- return buffer.toString();
- }
- }
复制代码
cmd 数组换成你要执行的命令即可! |
|