java如何生產執行bat腳本
bat文件內容如下:
cd <arg0>
java <-classpath arg1> <-jar> arg2 <arg3 arg4..>
說明:
<尖括弧里的不是必須的,按你的具體情況來>
arg0:你的class或jar文件所在目錄
arg1:你需要引入的jar文件,如果你已經在window的classpath里配置過或者不需要,那這句可以去掉
arg2:java類(*.class或*.jar)文件
arg3 arg4..:若干個參數,如沒有也可以去掉
前提是你安裝了jdk,並配置了環境變數
② java調用bat文件傳入參數,急,急,急!
java好像不能直接給bat文件傳參數,不過你可以先生成一個你需要的bat文件,再去執行這個bat文件,我就是這么做的,給你寫了個例子,你參考下(你先在d盤下建一個text.txt)
public class DelFile {
public void creatBat(String command){
FileWriter fw=null;
try {
fw=new FileWriter("D:\\del.bat");
fw.write(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}finally{
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
}
}
}
private String execute(String batname){
Process process;
String line = null;
StringBuffer sb=new StringBuffer();
try {
process = Runtime.getRuntime().exec(batname);
InputStream fis = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
if(process.waitFor()!=0){
System.out.println("fail");
return "fail";
}
System.out.println(batname+" run successful!");
return "success";
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DelFile df=new DelFile();
df.creatBat("del D:\\text.txt");
df.execute("D:\\del.bat");
}
}
③ Java中如何調用bat,並傳入參數
如果能動態指定bat文件中參數更好。例如: 1.bat中> java -cp Chart2D這里有調用windows程序的例子,你可以參考一下,就在調用的地方吧你的bat文件