Search code examples
javawindowsfilenetwork-shares

get Path with server ip instead of letter


I'am coding an application in java which reads all paths of directories and files. This is working already. To migrate those files I do need the absolute path, which is the Server IP instead of letter (on a Windows system).

I've tried following methods found within the file class:

System.out.println(file.getCanonicalPath());
System.out.println(file.getAbsolutePath());

Both methods print out the exact windows path instead of the server IP where I'm reading all paths and writing them into a txt file.


Solution

  • As you know Java is portable to allow you to write code that runs on multiple operating systems. When you call either of these methods Java is asking the underlying operating system what the path is for that file and returning what you are seeing. On a Windows machine this will be the path including the drive letter if it's a mapped (or local) drive. Sometimes you may get a UNC path, which includes the server name and share. For other operating systems this behavior will be slightly different because it'll return the path as that operating system sees it. However, Java does not actually know or understand anything about drive letters or the operating system directly.

    Therefore these methods cannot do what you are requesting. For Windows you sound like you're looking for a UNC path, which still won't provide the IP address, but instead will server name. They are in this format (and considered absolute):

    \\server_name\share_name\path\to\file.ext
    

    Usually a mapped drive will convert the \\server_name\share_name portion of a UNC path into a letter. So be aware that the letter does not necessarily directly map to the server, but to a share on a specific server (or Windows DFS share).

    In order to do this you will lose the portability that Java provides. Currently, the only way that I know how to retrieve the server name from the mapped letter drive is to do this with WMI. There are WMI libraries that Java can use; some use JNI and others use other scripting methods. WMI stands for Windows Management Instrumentation. This allows you to make SQL-like calls to Windows to query information about the system. In this case you'd need to parse out the drive letter being returned to you and then make the WMI call.

    Here's an article about using WMI to do this: How Can I Determine Which Drives are Mapped to Network Shares

    Just note that the above link will show you how to do it with VBScript, but the concept is the same.

    Here's another SO question about WMI interfaces for Java: Recommended libraries/howtos for using WMI with java?

    Once you use WMI to get the hostname of where the mapped drive is located, or if you were originally returned a UNC path and you parsed out the server name, then you can then call the following to get the IP:

    InetAddress address = InetAddress.getByName("COMPUTER_NAME"); 
    String ip = address.getHostAddress();
    

    One important thing to note is that to use WMI you sometimes need elevated permissions so just be aware of this possibility.

    Be prepared for this to be far more complicated than you expected it to be and a lot of string parsing to actually extract the data that you want.