How to define a Windows directory in ProcessBuilder directory() method?

M

minjie

I'm very new to Java, so please excuse me if the quesions are too
trivial.

I want to invoke a process with the following:
------
ProcessBuilder pb = new ProcessBuilder("crw32.exe",
"myreport.rpt");
pb.directory("C:\Program Files\Crystal Decisions\Crystal
Reports 9\"); // <-- error
Process p = pb.start();
------
But I got the error "Illegal escape character". If I change that
statement to:
pb.directory("C:\\Program Files\\Crystal Decisions\\Crystal
Reports 9\\");

I got the error:
"cannot find symbol.
symbol : method directory(java.lang.String)".

Isn't directory() a method in ProcessBuilder class (I'm using Java
1.5)? Is the error due to the fact that I still don't have the correct
path defined in there? How should I define that directory? Is it OK if
there are spaces in the path?

Also, how do I define the directory for the file "myreport.rpt" as
"C:\My Reports\", if I've already defined a directory to be "C:\Program
Files\Crystal Decisions\Crystal Reports 9" for the command "crw32.exe"?


Thanks.
 
J

Jean-Francois Briere

Isn't directory() a method in ProcessBuilder class...

The correct statement is:
pb.directory(new File("C:\\Program Files\\Crystal Decisions\\Crystal
Reports 9"));
Also, how do I define the directory for the file "myreport.rpt"...

Depends of what you want to achieve.
Must your program be executed where the 'myreport.rpt' is located?
If so then you should have 'C:\My Reports' as working directory and do:

ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Crystal
Decisions\\CrystalReports 9\\crw32.exe", "myreport.rpt");
pb.directory("C:\\My Reports");

Or maybe you want your program be executed in the software folder.
If that case you should have 'C:\Program Files\Crystal
Decisions\Crystal Reports 9' as working directory and do:

ProcessBuilder pb = new ProcessBuilder("crw32.exe", "C:\\My
Reports\\myreport.rpt");
pb.directory("C:\\Program Files\\Crystal Decisions\\CrystalReports 9");

Regards
 
M

minjie

Jean-Francois Briere said:
The correct statement is:
pb.directory(new File("C:\\Program Files\\Crystal Decisions\\Crystal
Reports 9"));


Depends of what you want to achieve.
Must your program be executed where the 'myreport.rpt' is located?
If so then you should have 'C:\My Reports' as working directory and do:

ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Crystal
Decisions\\CrystalReports 9\\crw32.exe", "myreport.rpt");
pb.directory("C:\\My Reports");

Or maybe you want your program be executed in the software folder.
If that case you should have 'C:\Program Files\Crystal
Decisions\Crystal Reports 9' as working directory and do:

ProcessBuilder pb = new ProcessBuilder("crw32.exe", "C:\\My
Reports\\myreport.rpt");
pb.directory("C:\\Program Files\\Crystal Decisions\\CrystalReports 9");

Regards

Hello Jean-Francois, thank you very much! pb.directory(new File("..."))
worked! I tried both methods in your examples. They both worked. I
guess for my application, I'll use your first example (i.e., use C:\My
Reports as working dir).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top