Javaを使用してコマンドラインまたはbatファイルを実行する

1031 ワード

public class Hellotianhao {

public static void main(String[] args) throws Exception{

	System.out.println("hello tianhao");

	Runtime.getRuntime().exec("cmd /k mkdir d:\\xutianhao");

}

}


実行結果は、dディスクにxutianhaoというフォルダが新規作成されました
 
 
 
 
JAva実行batファイル batファイルを書く注意各コマンドの前に@を1つ追加します
batファイル 
@e:

@ant


JAvaファイル
public class Hellotianhao {

public static void main(String[] args) throws Exception{

	

	

	Runtime.getRuntime().exec("E:\\firstbat.bat");

	

}

}


このjavaファイルを使用してbatファイルを呼び出してbatファイル内の2つの命令を実行します.
1.先にeディスクに切り替える
2.antツールを再起動してant命令を完了する(dirファイルを削除する)
 
構築ファイルbuild.xml
<?xml version="1.0"?>



<project name="targetStudy" default="deletedir">

<target name="deletedir" description="    ">

	<delete dir="newdir1"/>

</target>

</project>