Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mount a windows drive in Java?

We are working with some legacy code that accesses a shared drive by the letter (f:\ for example). Using the UNC notation is not an option. Our Java wrapper app will run as a service, and as the first step, I would like to map the drive explicitly in the code. Has anyone done this?

like image 365
Brett McCann Avatar asked Oct 16 '08 14:10

Brett McCann


People also ask

How do I map a network drive in Java?

Consider executing the DOS command that maps a network drive as in the following code: String command = "c:\\windows\\system32\\net.exe use f: \\\\machine\\share /user:user password"; Process p = Runtime. getRuntime(). exec(command); ...


2 Answers

Consider executing the DOS command that maps a network drive as in the following code:

String command = "c:\\windows\\system32\\net.exe use f: \\\\machine\\share /user:user password";
Process p = Runtime.getRuntime().exec(command);
...

See details on net use command:

The syntax of this command is:


NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
        [/USER:[domainname\]username]
        [/USER:[dotted domain name\]username]
        [/USER:[username@dotted domain name]
        [/SMARTCARD]
        [/SAVECRED]
        [[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]
like image 97
Jorge Ferreira Avatar answered Oct 28 '22 02:10

Jorge Ferreira


You can use JCIFS

http://jcifs.samba.org/src/docs/api/jcifs/smb/SmbFile.html

or if you want higher level API and support for other protocols like FTP, Zip and others:

http://commons.apache.org/vfs/filesystems.html

Both options are pure Java and cross platform.

like image 32
ddimitrov Avatar answered Oct 28 '22 03:10

ddimitrov