Enumerating attached DVD drives in Linux / Java / Scala

自古美人都是妖i 提交于 2019-12-08 06:56:20

问题


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:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/779088/enumerating-attached-dvd-drives-in-linux-java-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!