I am facing a problem with a .bat file run from Java web application.
Current setup in local machine: create web application and method contains
Process p = Runtime.getRuntime().exec("c:/test/myFile.bat");
When I run above code, its showing file not found
.
But if I copied the .bat file to tomcat bin folder it's working fine. But I need to execute the bat file in my separate folder, I even set the class path also, but I'm not able to execute the bat.
Screen 1:
I have placed 5 files in Tomcat root directory din folder, as well as placed at c:/psgsscripts/--> folder also (check the Java code; only bat file is taken other .tbc files are looking at Tomcat bin folder location at run time)
Screen 2:
bat file contains tclsh psg.tbc %1
Screen 3:
web application java code in class method( newjobid is the parameter for .bat file)
Screen 4:
If I placed it all .bat file and .tbc files bin folder it's working fine (Java code represent read the .bat file from c drive but other .tbc files look at Tomcat bin folder at run time)
but requirement is those files are placed at other drives (other than Tomcat folder) like C or D or E drivers.
您需要执行以下命令来运行批处理文件,如下所示
Runtime.getRuntime().exec("cmd /c start c:\\test\\myFile.bat");
Try the following:
Process p = Runtime.getRuntime().exec("cmd /C start c:/test/myFile.bat");
If you look at the java docs , the method exec expects an OS command and not the file name. So in the proposed solution, "cmd" is the OS command, /C
is a switch that tells the OS command to carry out the command specified by string and then terminate. Here the command specified by the string is start
. The command "start" requires a file name with full path as its parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.