Blog


Java and Samba (2011-08-08)

Overview

This article outlines a fairly simple solution to a potentially complex problem. This solution was derived in response to a requirement for a project of mine - a J2SE application that needed to be able to open folders on an Active Directory network file server. That is, when the user enacted a certain work flow in my application, it should then open a folder on the network drive in the native file browser of the host operating system; e.g. open in Finder, or Windows Explorer.

Solution

As of Java 1.6, this is quite easy to do if the host operating system is Windows using the java.awt.Desktop class. A simple code example illustrates this.

...
//code that opens an active directory folder in Windows Explorer
try{
  Desktop.getDesktop().open(new File("\\\\ServerName\\FolderName"));
}catch(Exception e){
  e.printStackTrace();
}
...

Pretty straight forward right? This code in true Java fashion also works on Unix based systems such as Mac OS X, and Linux. However this code will only work for local files and folders. The problem here with *nix platforms is that they must use Samba to access the files and folders of an Active Directory server. Java has no API to for which to control Samba. You can use Samba to mount a network drive on the local file system, and then you can use the code above to access those files and folders. However this requires the user to manually do this ahead of time in order for your code to work - not ideal.

Upon realizing this, I did some digging around. I didn't really find much, hence why I'm writing this article. There does exist what appears to be a good Samba library for Java - JCIFS. Presumably using this library you could write code to mount a network volume, and then use the code above from there. I downloaded this library, example code and all. It wasn't as straight forward as I hoped it might be, I could tell it would be a significant investment of time in the solution.

After playing around a little bit with a few different operating systems, several cups of tea, and some self-induced hair pulling, I realized that a Samba URL is valid in modern browsers such as Safari and FireFox. Things get much simpler from here. Also residing within the Desktop class is a method for opening URLs in a user's default browser from within Java. Using this method you can open an Active Directory volume from within Java on a *nix operating system without the user having to manually mount the volume first. Some sample code to demonstrate this...

...
/*This code takes a Windows Active Directory folder path in the format of '\\ServerName\FolderName' and opens it in the native OS file browser.*/
try{
  String path = "\\\\ServerName\\FolderName";
  if(File.separator.equals("/")){//*nix
    Desktop.getDesktop().browse(new URI("smb:"+path.replaceAll("\\\\","/").replaceAll(" ","%20")));
  }else{//Windows
    Desktop.getDesktop().open(new File(path));
  }
}catch(Exception e){
  e.printStackTrace();
}
...

I hope this solution helps a few of you on your way.

Valid XHTML 1.0 Strict    Valid CSS!
© Copyright Griffin Software 2013