0

I know that questions like this have been asked before, but I couldn't find any more recent ones and I have a twist to my question.

I've developed an application in Java that is designed to run on removable media, and work on both Windows and Mac. I would like to add a button to safely remove/eject the device, if it is supported (i.e a USB drive). Is there a command line for each operating system that would allow me to do this?

I know that it can be done by an application running on the device to be ejected, because I've seen one that does it, but obviously I understand there are certain limitations to Java.

Thanks in advance

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Andy
  • 3,600
  • 12
  • 53
  • 84

1 Answers1

2

This is something that you will have to do by invoking an auxiliary application. These applications are not platform independent as you wish. So, to do that, find out which OS you are on by using System.getProperty("os.name") and invoke the appropriate command for the detected OS. Invoking applications is done with Runtime.getRuntime().exec(). Search for the commands you need for each OS.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • Thanks for your answer. Do you know if there is any other way to eject a drive on Windows other than `diskpart` (without using an external utility)? Also, will the disk be ejected if my program is still running and was launched from it? – Andy Aug 27 '13 at 19:03
  • No, I'm not a Windows user. Just search and test until it works. For the second question, just make sure there aren't any file streams opened anymore to the disk. – Martijn Courteaux Aug 27 '13 at 19:12
  • Ok thank you. That's fair enough – Andy Aug 27 '13 at 19:16
  • "devcon" command is able to disable and enable a drive on Windows but it requires WinSDK to be installed first. Check this for alternative: http://stackoverflow.com/a/39081086/3780985 – Luke Aug 22 '16 at 21:38