3

How can I mount and unmount hard drives (platform independent, so no using Runtime to execute a hard-coded command) in the Java Programming Language?

4 Answers4

6

The answer is "Yes And No". You could not mount or unmount devices in java because each OS has their own methods to do this. BUT... you can provide java api that use adapter pattern for native interface. You should do some things:

  • create Java interfaces that support mount/unmount commands
  • create classes that implements interfaces as native methods
  • create native implementations of this commands in C or other language. One implemantation for OS (Win, Mac, Linux)
  • pack it to one jar
  • build small factory that provide implementation of interface and load native libraries

It is not pure java, but is good solution I think. Client code will use java.

Koziołek
  • 2,791
  • 1
  • 28
  • 48
1

As there is no way to do this portably (or even with the same semantics), there is no built-in Java method to do so.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
1

"Platform independent" doesn't mean you are not allowed to use external processes. You can check the OS your app is runnning on. See System.getProperty("os.name"); Depending on the result of that method, invoke the correct process.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • I know but I was only saying that in hope that there was a feature in the JDK that can do it for me. –  Mar 31 '11 at 20:33
0

Think about it: Java is supposed to work on platforms where no hard drives exist, so how could there be a platform independent way of doing this?
Besides, even if you restricted it to platforms where hard disks are available, how is Java supposed to abstract away the different approaches to RAIDs / partitions / slices etc?

Why do you want/need to do this?

Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
  • Java is supposed to work on platforms where no hard drives exist? –  Mar 31 '11 at 20:14
  • I want to do this because I was looking into creating my own hard drive encryption program (sort of like TrueCrypt). –  Mar 31 '11 at 20:15
  • 2
    Java has lots of features that can't be implemented by all supported platforms, like AWT and Swing (think about "headless" server machines). Just because all target platforms can't implement some group of APIs doesn't mean Java can't provide them... – maerics Mar 31 '11 at 20:27
  • @Gnarly: I was thinking of Smartphones, embedded devices etc. - but I guess Java SE/EE (which we are talking about here, I guess) runs only on devices with hard disks or similar. So my answer was kind of misleading, sorry about that. – Frank Schmitt Apr 01 '11 at 06:39