Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes "system cannot find the file specified" with Runtime.exec? (assoc/ftype)

Tags:

java

exec

Does anyone know why these two statements result in the shown exception?

Runtime.getRuntime().exec("assoc .vlan=\"file type description\"");
Runtime.getRuntime().exec("ftype \"file type description\"=" + System.getProperty("user.home")+ "\\folder 1\\folder 2\\my executable.exe\" /inject \"%1\"");

And Here's the Exception.

java.io.IOException: Cannot run program "assoc": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)

All right, I've changed this but still the association is not made whereas the very same command is executable and working under the cmd command prompt.

Runtime.getRuntime().exec("cmd.exe /c assoc .vlan=\"file type description\"");
Runtime.getRuntime().exec("cmd.exe /c ftype \"file type description\"=\"" + System.getProperty("user.home") + "\\folder 1\\folder 2\\my executable.exe\" /inject \"%1\"");

Any complementary suggestion? Thank you!

like image 635
Sam Avatar asked Dec 28 '22 09:12

Sam


1 Answers

As it turns out, assoc and ftype are built-in shell commands and NOT executable files. The same is true for copy, dir, etc. What you can do instead is launch cmd.exe using the /c parameter to pass a command line string, e.g. cmd.exe /c assoc ....

like image 70
bobbymcr Avatar answered Feb 13 '23 22:02

bobbymcr