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文件