http://madnix.tistory.com/m/post/entry/Java에서-Linux-Shell-명령어-실행하기

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
 
public class ShellCommander {
 
  public static void main(String[] args) throws Exception {
 
                String command = "ls -al";  
               // 이 부분에 실행할 리눅스 Shell 명령어를 입력하면 된다. (여기선 ls -al 명령어 입력)
               shellCmd(command);
 
   }
 
   public static void shellCmd(String command) throws Exception {
 
               Runtime runtime = Runtime.getRuntime();
               Process process = runtime.exec(command);
               InputStream is = process.getInputStream();
               InputStreamReader isr = new InputStreamReader(is);
               BufferedReader br = new BufferedReader(isr);
               String line;
 
               while((line = br.readLine()) != null) {
                              System.out.println(line);
               }
   }

}

 
 
==========
 
 

<%@ page language="java" 
   import= "java.io.*,  
   java.util.*"  
   contentType="text/html;charset=EUC-KR" session="false"  
%> 

<html> 
<% 

    String command = "ls -al";  // <---- 실행할 쉘명령어 
    int lineCount = 0; 
    String line=""; 

    Runtime rt = Runtime.getRuntime(); 
    Process ps = null; 

    try{ 
      ps = rt.exec(command); 

      BufferedReader br = 
            new BufferedReader( 

                    new InputStreamReader( 

                          new SequenceInputStream(ps.getInputStream(), ps.getErrorStream()))); 

             

      while((line = br.readLine()) != null){ 
%> 
    <%=line%><br> <!-- 결과 화면에 뿌리기... --> 
<% 
      } 
      br.close(); 

   }catch(IOException ie){ 
      ie.printStackTrace(); 
   }catch(Exception e){ 
      e.printStackTrace(); 
   } 
%> 
</html> 

 

Posted by 張's blog
,