자바 프로세스 실행
자바 프로세스 실행 간단한 방법
import java.io.IOException;
public class ProcessTest
{
public static void main( String args[] )
{
try
{
Process p1 = Runtime.getRuntime( ).exec( "calc.exe" );
Process p2 = Runtime.getRuntime( ).exec( "Notepad.exe" );
p1.waitFor( ); // 자식 프로세스가 종료될 때까지 기다립니다.
p2.destroy( ); // 부모 프로세스에서 자식 프로세스를 강제로 종료시킵니다.
}
catch ( IOException e )
{
e.printStackTrace( );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
System.exit( 0 ); // 부모 프로세스만 종료되고 자식 프로세스는 계속 실행됩니다.
}
}
명령어를 동시에 실행 시키고자 한다면 '명령어 & 명령어' 를 사용
mssql 백업에 이용
| import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Backup { public static void main( String[] args ) { Runtime rt = Runtime.getRuntime( ); Process p = null; try { p = rt.exec( "cmd /c bcp \"SELECT * FROM tb.dbo.TABLE WHERE YY='12' AND MM='02' AND DD='02'\" queryout c:\\temp\\back.sql -c /Useban /Pseban00" ); InputStream in = p.getInputStream( ); InputStreamReader isr = new InputStreamReader( in ); System.out.println( isr.getEncoding( ) ); BufferedReader br = new BufferedReader( isr ); String line = null; while ( ( line = br.readLine( ) ) != null ) { System.out.println( line ); } in.close( ); } catch ( IOException e ) { e.printStackTrace( ); } } } |
public int ExecuteCmd(String[] s){
long start=System.currentTimeMillis();
int val=0;
try{
Process process=new ProcessBuilder(s).start();
BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream());
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
process.waitFor();
val=process.exitValue();
}catch(Exception){
val=99;
}finally{
long end=System.currentTimeMillis();
return val;
}
}