tags:

views:

253

answers:

1

How can I "eject" a volume with Java, cross platform?

I have a program that does some operations on a removable drive (USB memory card reader), and once it's done, I want the program to eject/unmount/remove (depending which os lingo we're talking in) the memory card.

Is there a reliable cross-platform method of doing this?

+2  A: 

Probably isn't the answer you're looking for, but...

No.

To my knowledge, there isn't an established single-platform way of doing this. For that matter, I've never come across a Java way of doing this. A rather scary C# CodeProject does allow ejecting devices, but only on Windows.

The various, depressingly poor, Java USB libraries don't even hint at ejecting devices. They don't work across all platforms, so even if they did it wouldn't help you.

My suggestion: gin up some scripts or executables for each platform, and then just spin up a Process as needed.

Kevin Montrose
Or wrap some OS-specific code in JNI. But I'm sure this is the 'hard way'.
Peter Dolberg
I'd only consider JNI if performance is important. If 'eject usb' is in an inner loop somewhere, it might be worth it. Then again, that'd raise... interesting questions.
Kevin Montrose