File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes problem of brackets running file bat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem of brackets running file bat" Watch "problem of brackets running file bat" New topic

problem of brackets running file bat

ed lapoign´┐Że

Joined: Aug 20, 2004
Posts: 1

I have a problem in my java code when I want run a file bat whom the name have brackets (Ex: VAL(SAU)-AccType01.bat)

My java code is:
File f = new File(pathFileBat);
run = Runtime.getRuntime();
process = run.exec("cmd /c start " + bat, null, pathClassCommand);

The following message appears:
VAL is not recognized as an internal or external command..

If i do:
process = run.exec("cmd /c start \"" + f + "\"", null, pathXX);

The windows appears correctly(opening of the good repertory(pathXX)) but the file bat isn't run

Thank you to help me
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
if in doubt - cheat!
Im assuming commandLine contains the shell like for the bat file


it means the commandLine variable is written out to the StdIn for the Shell
It should work - but i dont have a bat to check!

Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
I believe that part of what Tom's suggesting is actually the right way to go about it, rather than cheating. You should open the command window with getRuntime().exec() as you're doing, setting the working directory, and then write your commands to the shell's stdin.

You should really handle stdout and stderr too as failing to do so can easily cause the process to hang. If you search this forum for Runtime.getRuntime() there's a recent thread in which I included a link to a article with source code to do this.

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Intermediate forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: problem of brackets running file bat
It's not a secret anymore!