Change Directory in Java Runtime.exec
Java's security is extremely strict. Whatever Windows batch file you execute, with the Runtime.exec () method, can't determine the current working directory, nor can it change directories. But here is a trick to get your batch files to work if you really have to change working directories . . .
Have Runtime.exec () called a batch file that in turn calls another batch file with the "command /c" directive. For example, if you have "Launcher.bat" and "Execution.bat", Put the following line in "Launcher.bat":
command /c Execution.bat
"Execution.bat" will be able to look at its current working directory and change it as well.
The two batch files shown above are conceptual. For my testing, I actually used "Prototype.bat" and "Test.bat". "Prototype.bat" calls "Test.bat" with "command /c".
When I tried to implement the solution using "Launcher.bat" and "Execution.bat", I kept getting the "Bad command or file name" error message.
Based on further testing, I found that "command" can't accept file names greater than eight characters. It executes names like "Test.bat", "hello.bat", and "electric.bat" just fine. But it chokes if it tries to execute file names like "Execution.bat" or "helloworld.bat".
It seems that changing directory is only a problem if you execute batch files with the ".bat" extension. If you execute batch files with the ".cmd" extension, you won't have this problem. Possibly a different command processor (command.com vs. cmd.exe) is used.
Also, the short file name problem is only a problem for command.com (16-bit command processor). It's not a problem for cmd.exe (32-bit command processor).
Did your message disappear? Read the Forums FAQ.
Spam Control | * indicates required field
No TrackBacks yet. TrackBack can be used to link this thread to your weblog, or link your weblog to this thread. In addition, TrackBack can be used as a form of remote commenting. Rather than posting the comment directly on this thread, you can posts it on your own weblog. Then have your weblog sends a TrackBack ping to the TrackBack URL, so that your post would show up here.
Messages, files, and images copyright by respective owners.
12 Users Online
Copyright © 1996 - 2017. All Rights Reserved.