在java中我用这种方式调用批处理(bat)文件
String batFilePath = "cmd /c d:/workshop/set/startup_setdb.bat";
try{
Process process = Runtime.getRuntime().exec(batFilePath);
InputStream in = process.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(in));
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
try{
process.waitFor();
br.close();
in.close();
}catch ( IOException e) {
MsgBox.showError(null, e.toString());
return false;
}catch (InterruptedException e) {
MsgBox.showError(null, e.toString());
return false;
}
}catch (IOException e) {
MsgBox.showError(null, e.toString());
return false;
}
return true;
}
该java程序和bat文件在XP下可以都可以正常使用;
但是在win7通过该java程序调用bat文件下就不行了;
win7下, 未发现数据源名称并且未指定默认驱动程序 org.hibernate.exception.GenericJDBCException 通过手动的形式调用bat,并且选择“以管理员的身份运行”可以正常运行批处理文件,说明bat文件是没有问题的。 antlr.TokenStreamIOException
String batFilePath = "cmd /c d:/workshop/set/startup_setdb.bat";
try{
Process process = Runtime.getRuntime().exec(batFilePath);
InputStream in = process.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(in));
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
try{
process.waitFor();
br.close();
in.close();
}catch ( IOException e) {
MsgBox.showError(null, e.toString());
return false;
}catch (InterruptedException e) {
MsgBox.showError(null, e.toString());
return false;
}
}catch (IOException e) {
MsgBox.showError(null, e.toString());
return false;
}
return true;
}
该java程序和bat文件在XP下可以都可以正常使用;
但是在win7通过该java程序调用bat文件下就不行了;
win7下, 未发现数据源名称并且未指定默认驱动程序 org.hibernate.exception.GenericJDBCException 通过手动的形式调用bat,并且选择“以管理员的身份运行”可以正常运行批处理文件,说明bat文件是没有问题的。 antlr.TokenStreamIOException
所以我怀疑就是win7中使用java调用bat文件不能够以管理员的身份运行bat文件,导致bat文件中的有一部分内容没有执行权限。
解决方案:
NirCmd是一套免费的命令行指令,提供许多控制Windows的参数。让你运行命令行的方式,来执行一些常用的动作,例如开启或关闭光驱,增加或减少计算机音量。让你在执行一些常用的动作时,可以更加的有效率。
3.2 Elevate指令说明
elevate [Program] {Command-Line Parameters}
仅适用于 Windows Vista/7/2008 ,以管理员权限运行。如果Program 包含空格符,则需要用引号括起来。
示例:
elevate notepad.exe
elevate notepad.exe C:\Windows\System32\Drivers\etc\HOSTS elevate "c:\program files\my software\abc.exe"
3.3 使用说明
1. 将nircmd.exe拷贝进工程下的指定目录
2. 在JAVA代码中用Runtime.getRuntime().exec()执行以下指令:指定目录/
nircmd.exe elevate net stop/strat “FileZilla Server”