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 Working with ProcessBuilder's working directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Working with ProcessBuilder Watch "Working with ProcessBuilder New topic

Working with ProcessBuilder's working directory

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
I'm trying to run executable windows installer files from Java using ProcessBuilder. I've got it to work when the program is in the same directory as the java program, but when I try changing the working directory using ProcessBuilder's directory(File directory) method, my program fails. Here's my code so far:

So then I create an executable jar, copy it to the desktop, and run it. I have a folder called "Standard Software" on my desktop with a program labeled "Flash_IE_32.exe" in it. When I run the jar, it finds that the directory exists, displays the current working directory, and then throws an exception when trying to start the process. I copied that installer to the desktop, and then my program runs fine. So my question is, how do I REALLY change the working directory? Or how do I run a file from any other directory, since obviously changing the working directory is completely useless (unless I'm doing something wrong, which I realize that I am )?

Studying for SCJP 6
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Eric Daly wrote:I have a folder called "Standard Software" on my desktop with a program labeled "Flash_IE_32.exe" in it.

But on the other hand, you're trying to run that program out of this directory:

which surely isn't the one on your desktop?
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Well, when I run the jar from the desktop, it finds that directory and displays a message stating that the working directory is C:\Users\Eric\Desktop\Standard Software
Based on my understanding, user.dir is giving me my current directory, wherever I run my program from. So yes, I'm expecting it to run that program that's in the standard software folder on the desktop. For some reason it's still trying to run the program directly from the desktop, even though I clearly have changed its working directory to standard software and verified it. I really don't understand what I'm doing wrong here, I already tried explicitly setting the directory manually, but it still does the same thing. What am I missing?
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
So it turns out that you have to specify the path (at least in my case) when creating the ProcessBuilder object (versus changing the working directory later).

If you create the ProcessBuilder object first, and then try changing the working directory, apparently the program is still going to try to run from wherever the ProcessBuilder object was created, unless you specify the path at that point. If that really is the case, then in all honesty I just don't understand what the point of changing the working directory is. I mean, isn't it completely useless? Can somebody please explain what it actually DOES? It doesn't do anything for me... I don't mind, as long as there's a way to run programs outside of the current directory (which I finally found), but I just want to know what is really happening behind the scenes of ProcessBuilder. I've googled this to death, and can't find any clear cut examples (you've gotta admit, the examples from Sun aren't the best).

So... what's VAR1, myValue, OTHERVAR, VAR2, and suffix? Am I really just completely missing the point here, or can somebody else testify that this example is pretty confusing? I need more documentation...
I agree. Here's the link:
subject: Working with ProcessBuilder's working directory
jQuery in Action, 3rd edition