I’m trying to eject a USB thumb drive on Mac from a Java program. I’ve tried umount and diskutil eject and neither works. I get an exit code of 1 from both. If I physically pull the drive out, Mac OS X gives me the standard warning.
My code:
log.debug( "going to eject targetRoot.getPath()=" + targetRoot.getPath() );
String command = "diskutil eject " + targetRoot.getPath().replace( " ", "\\ " );
log.debug( "about to run command=" + command );
int exitCode = Runtime.getRuntime().exec( command ).waitFor();
log.debug( "exitCode=" + exitCode );
The output:
going to eject targetRoot.getPath()=/Volumes/NO NAME
about to run command=diskutil eject /Volumes/NO\ NAME
exitCode=1
The man pages indicate that you can use the mounted path or the path to the device as an argument for umount or diskutil eject.
If I copy the command to the terminal and run it, then it works perfectly. I suspect it’s something related to the environment, but as you can see I’m not sending an envp to the exec method.
What’s a programmer gotta do to make the eject/unmount work?
Thanks!
Have you tried using ProcessBuilder rather than Runtime.exec()? It lets you specify each of the arguments precisely, and it also lets you construct an environment.
…or at least use the form of Runtime.exec() specifying an array of arguments rather than a string.
I would guess that your escaping of spaces isn’t working — the convenience form of Runtime.exec() handles spaces using StringTokenizer and probably isn’t as smart as most shells.