我用exec来执行一个.bat批处理文件,如果直接运行程序,就实现不了我想要的。也就是和直接双击bat文件的效果不同。而如果用debug模式,执行,就可以!执行函数如下:大家看有什么问题吗public static void openExe(String path) {
Runtime rn = Runtime.getRuntime();
try {
rn.exec(path);
} catch (Exception e) {
System.out.println("Error exec!");
}
}
Runtime rn = Runtime.getRuntime();
try {
rn.exec(path);
} catch (Exception e) {
System.out.println("Error exec!");
}
}
二则,你说的效果不同是什么?
net share a="%apppath%\hotfolder"
我想要的效果就是,通过java的exec执行完这个bat文件后,将对应位置的那个名为hotfolder的文件夹设置成共享。
你这样不行的
1) .bat是在cmd下运行的
2) 不能把路径带进去,路径要另外指定举例来说,假如server文件夹在C盘根目录下,这样写:
rn.exec("cmd /k start hotfolder.bat", null, new File("c:/server/"));
带/k参数是为了让执行完后cmd窗口留下来便于你观察。
Java执行exe,bat等可执行文件的实现代码:Process proc = Runtime.getRuntime().exec(command); import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CallExe {
public static void main(String[] args) {
String text = null;
String command = "C:Program FilesYodaoDeskDictRunDict.exe";//exe,bat文件名OR DOS命令
try {
Process proc = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((text = in.readLine())!= null) {
System.out.println(text); //输出测试
}
} catch (IOException ioError) {
ioError.printStackTrace();
System.exit(0);
}
}
这样,直接把command用path参数来代替就能正确运行!谢谢大家,结贴了!