Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access a shared folder on Windows system from Java application on Unix

 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I am working on a Java application that accesses a shared folder on a Windows system.For example in my Java code I access a file called 'myfile.txt' contained in a shared folder called 'common'.
So in my code I give the folder path as:
\\common\\myfile.txt
Now I have created a jar out of this application and deployed it on Solaris system.But it obviously isn't working because Solaris system isn't able to access the 'common' folder.
How do you do this?What change is required for Java code on Unix to be able to access shared folder on Windows machine?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The jCIFS library can be used to access Windows Shared directories. Its API mimics the File and File[In|Out]putStream classes in java.io, so you can pick it up very quickly.
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No change is required in Java code.

In your example you access a normal file system file "\\common\\myfile.txt". This is normal file like "c:\\myfile.txt". Java doesn't know it is shared, it thinks it is just a file.

On unix, file system is a little bit different. It is possible to mount shared folder "\\common\" on a local directory using SAMBA. E.g. you mount "\\common\" to "/mnt/common/" and then load file "/mnt/common/myfile.txt" from your java app.

http://en.wikipedia.org/wiki/Samba_software
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vilmantas Baranauskas:
In your example you access a normal file system file "\\common\\myfile.txt". This is normal file like "c:\\myfile.txt". Java doesn't know it is shared, it thinks it is just a file.

You should use "\\\\common\\myfile.txt", since you also need to escape the first two \s.

Sometimes I miss that in Java - the raw string like Python has it. No need for escaping anything in those.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic