views:

394

answers:

2

In my Scala (runs on top of Java) Application I would like to get a list of all drives that contain DVD media, e.g. something like this:

  • /dev/scd0 Star Trek DS9 DVD1
  • /dev/scd0 The 4400 DVD1

Not sure if it's possible to get the name of the disc, but the path is the important thing for me anyway.

I would prefer a pure Java / Scala solution (using file.io stuff). If that's not possible, accessing the right Linux files is fine, too (like /proc/something).

Thanks in advance!

+1  A: 

I think you're out of luck with java.io.* but if you don't mind making calls out to Linux commands, you could assemble the data by:

  1. Calling "mount" and capturing the first column of output.
  2. Calling "volname" on each value you captured from step 1.

According to the man page for volname, it only returns data for ISO-9660 filesystems (e.g. DVDs), so any device path that returns empty can be ignored.

Rob H
A: 

There is one (untested) possibility to get your drives with pure Java code. At least on Windows.

Its a little bit hacky and doesn't work under linux (because linux gets not as much integration love from sun I believe).

import javax.swing._
import javax.swing.filechooser._
val chooser = new JFileChooser()
val view = chooser.getFileSystemView()

The FileSystemView clas provides a few features such as asking the possible roots if they are a drive (isDrive()). Swing uses this to present the file chooser with the right icons to you so it should work under windows because IIRC it shows the correct symbols there. Under Linux it unfortunately does only show the "/" root.

One of the reasons this doesn't work under linux could be, that the linux developers constantly change their preferred way of presenting such information to the user space. at the moment it is IIRC hal and dbus. Maybe SUN didn't want to publish a new java version each time this changes.

If pure java doesn't cut it maybe you could use a little bit of jni (which is not so hard to use anymore if you're using tools like JNA or such) to access the linux apis directly. I haven't done that but could try if you're interested.