How can I mount a windows drive in Java?

976

Solution 1

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}]

Solution 2

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.

Solution 3

I think the easiest way is to use the Runtime.getRuntime().exec() method and call the "net use" command.

For example:

    try {
        // Execute a command without arguments
        String command = "C:\\Windows\\system32\\net.exe use F: \\\\server\\share /user:user password";
        Process child = Runtime.getRuntime().exec(command);
    } catch (IOException e) {
    }
Share:
976
Mojo
Author by

Mojo

Updated on October 14, 2020

Comments

  • Mojo
    Mojo over 3 years

    I'm trying to get all data exist in ipconfig /all for a specific interface.

    I can not use netssh, or any other program.

    I can use grep(for windows - but only grep, not mgrep pcgrep or whatever), sed (same thing..) or findstr of course.

    Can you guys think of a way?

    I want to get all the data for "SLOT 2 3" and loose all the other data.

    This is what the output looks like:

    Windows IP Configuration

       Host Name . . . . . . . . . . . . : l-075
       Primary Dns Suffix  . . . . . . . : 
       Node Type . . . . . . . . . . . . : Hybrid
       IP Routing Enabled. . . . . . . . : No
       WINS Proxy Enabled. . . . . . . . : No
       DNS Suffix Search List. . . . . . : wininstall.what.com
    

    Ethernet adapter SLOT 2 3:

       Connection-specific DNS Suffix  . :
       Description . . . . . . . . . . . : Ethernet Adapter
       Physical Address. . . . . . . . . : 00-02-C9-B5-94-E1
       DHCP Enabled. . . . . . . . . . . : Yes
       Autoconfiguration Enabled . . . . : Yes
       Link-local IPv6 Address . . . . . : fe80::202:c9ff:feb5:94e1%24(Preferred)
       IPv4 Address. . . . . . . . . . . : 12.0.0.33(Preferred)
       Subnet Mask . . . . . . . . . . . : 255.255.0.0
       Lease Obtained. . . . . . . . . . : Thursday, January 2, 2014 12:26:18 PM
       Lease Expires . . . . . . . . . . : Friday, January 10, 2014 12:26:18 PM
       Default Gateway . . . . . . . . . :
       DHCP Server . . . . . . . . . . . : 12.0.0.83
       DHCPv6 IAID . . . . . . . . . . . : 436208329
       DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1A-25-9E-19-D4-AE-52-A1-37-E7
       DNS Servers . . . . . . . . . . . : 10.4.0.0
                                           10.4.0.1
       NetBIOS over Tcpip. . . . . . . . : Enabled
    

    Ethernet adapter SLOT 2 2:

       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
       Description . . . . . . . . . . . : Ethernet Adapter #2
       Physical Address. . . . . . . . . : 00-02-C9-B5-94-E0
       DHCP Enabled. . . . . . . . . . . : Yes
       Autoconfiguration Enabled . . . . : Yes
    

    Ethernet adapter NIC4:

       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
       Description . . . . . . . . . . . : Broadcom NetXtreme Gigabit Ethernet #4
       Physical Address. . . . . . . . . : D4-AE-52-A1-37-EA
       DHCP Enabled. . . . . . . . . . . : Yes
       Autoconfiguration Enabled . . . . : Yes
    

    This is what I need:

    Ethernet adapter SLOT 2 3:

       Connection-specific DNS Suffix  . :
       Description . . . . . . . . . . . : Ethernet Adapter
       Physical Address. . . . . . . . . : 00-02-C9-B5-94-E1
       DHCP Enabled. . . . . . . . . . . : Yes
       Autoconfiguration Enabled . . . . : Yes
       Link-local IPv6 Address . . . . . : fe80::202:c9ff:feb5:94e1%24(Preferred)
       IPv4 Address. . . . . . . . . . . : 12.0.0.33(Preferred)
       Subnet Mask . . . . . . . . . . . : 255.255.0.0
       Lease Obtained. . . . . . . . . . : Thursday, January 2, 2014 12:26:18 PM
       Lease Expires . . . . . . . . . . : Friday, January 10, 2014 12:26:18 PM
       Default Gateway . . . . . . . . . :
       DHCP Server . . . . . . . . . . . : 12.0.0.83
       DHCPv6 IAID . . . . . . . . . . . : 436208329
       DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1A-25-9E-19-D4-AE-52-A1-37-E7
       DNS Servers . . . . . . . . . . . : 10.4.0.0
                                           10.4.0.1
       NetBIOS over Tcpip. . . . . . . . : Enabled
    
  • Jorge Ferreira
    Jorge Ferreira over 15 years
    If the user has already mapped drive f: you would get the "System error 85 has occurred." in the process output. You can just scan for it.
  • munsingh
    munsingh almost 9 years
    Directly using net.exe does not handle error cases and is not platform neutral.
  • Engineer2021
    Engineer2021 about 5 years
    @munsingh: He asked for Windows. Who cares for cross platform
  • Shubham
    Shubham over 3 years
    After the mounting, we can perform operations on the directory using normal FS commands right? Can you add an example to read a file? When I do Path share = Paths.get(new URI("f:\")); i get an error Provider "f" not installed.