Search code examples
javalinuxfilejava-iomount

In Java code running on Linux, how to point to a shared Windows folder?


I have a shared Windows folder:

\servername.domain.com\dir\warname

This is how I access it from Java code running on Windows OS:

// \dir
String directory = getInitParameter ("directory");

// /warname
final String contextPath = this.getServletContext().getContextPath(); 

// \dir/warname
directory += (directory.endsWith("/") ? contextPath.substring(1) : contextPath); 

// //servername.domain.com/\dir/warname

directory = "//" + getDatabaseServerName() + (directory.startsWith("/") ? "" : "/") + directory;

// \\servername.domain.com\dir\warname
File shareDir = new File(directory);

if (!shareDir.exists()) {
        if (!shareDir.mkdirs()) { 
            throw new Exception ("Error: " + shareDir + " does not exist and could not be created.");
        }
    }

From this point forward, I can access the \servername.domain.com\dir\warname and write to it.

If this code runs on Linux server, this is what I get:

directory: //servername.domain.com/\dir/warname

shareDir: /severname.domain.com/\dir/warname

And then the above exp. will be thrown:

java.lang.Exception: Error: /servername.domain.com/\dir/warname does not exist and could not be created.

So it tries to create a new dir and it fails.

How can I point to the same shared Windows folder from Linux?

I did Google search it but was not able to find the solution. Any help is greatly appreciated.


Solution

  • You may need to install the SMB client on Linux. See this article on how to do that: http://www.howtogeek.com/176471/how-to-share-files-between-windows-and-linux/