8

I'm using JCIFS in my new Android project. Somehow I've decided to use URL class to generalized the file path (so I can add more protocol later). What I did is as below

URL url = new URL("smb://192.168.1.1/filepath");

And then java.net.MalformedURLException exception is thrown.

Exception in thread "main" java.net.MalformedURLException: unknown protocol: smb
    at java.net.URL.<init>(URL.java:184)
    at java.net.URL.<init>(URL.java:127)

Consulting JCIFS FAQ reveals that I have to register the protocol before using the class. However I don't really know how to do so in Android. I think the library do this already, but it doesn't on Android.

So what should I do ?

mr_tawan
  • 666
  • 1
  • 5
  • 8
  • Where did you put the jcifs Jar file? It should be in the proper classpath for the Handler to be loaded. – mohdajami Jul 22 '12 at 09:05
  • I put the jar file in the /libs directory of the project, which I think it's not in the classpath. And I think Android handles classpath differently than the normal Java application do. – mr_tawan Aug 05 '12 at 20:09

2 Answers2

8

I've just saw the usage in JCIFS reference, in the SmbFile reference.

When using the java.net.URL class with 'smb://' URLs it is necessary to first call the static jcifs.Config.registerSmbURLHandler(); method. This is required to register the SMB protocol handler.

So I add this call and it works properly.

mr_tawan
  • 666
  • 1
  • 5
  • 8
  • Javadoc: [http://stderr.org/doc/libjcifs-java-doc/api/jcifs/Config.html#registerSmbURLHandler()](http://stderr.org/doc/libjcifs-java-doc/api/jcifs/Config.html#registerSmbURLHandler()) – Chris Parton Jul 22 '14 at 02:25
  • Previous link is not available, use https://jcifs.samba.org/src/docs/api/jcifs/Config.html#registerSmbURLHandler%28%29 instead – jneira Dec 15 '16 at 09:45
0

Don't use a URL object. Pass the URL directly into SmbFile constructor. For example:

SmbFile file = new SmbFile("smb://192.168.1.1/filepath");

Then you can do most everything you can do with a regular File.

11101101b
  • 7,679
  • 2
  • 42
  • 52
  • 1
    The reason I use URL object is I want to support other protocol (and local file system) too. Using URL can make it become more abstract among protocol. Also I want to use Serilizable object to pass between Activities, which SmbFile is not. – mr_tawan Aug 05 '12 at 20:06