Reputation: 9935
How to execute .bat
from classpath
? I can only execute .bat
the specific directory like D:\temp\kill-chrome.bat
.
ClassPath Directory
+ src
+ -> .java files ....
+ resource
+ kill-chrome.bat
My Program does not work.
String path = "cmd /c start /resource/kill-chrome.bat";
Runtime rn = Runtime.getRuntime();
try {
Process pr = rn.exec(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 0
Views: 796
Reputation: 2471
I think the proper way to get the path of a file in your classpath would be something like this:
URI uri = YourClass.class.getResource("resource/kill-chrome.bat").toURI();
String path = new File(uri).getAbsolutePath();
This aproach would go from your working directory (and this could differ):
new File("resource/kill-chrome.bat").getAbsolutePath();
Upvotes: 0
Reputation: 9935
I got simplest way which is using file.getAbsolutePath()
method. I am not sure which is suitable way or not.
String realPath = new File("resource/kill-chrome.bat").getAbsolutePath();
String path = "cmd /c start " + realPath;
Runtime rn = Runtime.getRuntime();
try {
Process pr = rn.exec(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In runtime.exec(commend)
, cannot directly to access the classpath
?
Upvotes: 1