使用java对执行命令行 或 执行bat文件

时间:2023-03-09 08:23:51
使用java对执行命令行 或 执行bat文件
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文件书写注意在每条命令之前都要加一条@

bat文件 

@e:
@ant

java文件

public class Hellotianhao {
public static void main(String[] args) throws Exception{ Runtime.getRuntime().exec("E:\\firstbat.bat"); }
}

  通过使用这个java文件调用bat文件执行bat文件内的两条指令

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>