Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Read file from a shared location Windows? (Java)

Tags:

java

file

Is there a way to read a file from a network shared location on windows?

Let's say for instance I've this simple code that reads a text file called readMe.txt from the Addons folder.

import java.io.File;

class Sample{

    public static void main(String[] ar){
        File file = new File("Addons/ReadMe.txt");
        System.out.println(file.getAbsolutePath());
      //followed by printing the contents of file   
    }
}

And I execute this file using a windows batch runme.bat that has

java Sample
PAUSE

The bat runs and executes the above class only when I place the Addons folder with ReadMe.txt, Sample.class, runme.bat file in my local drive.

When it is placed in a network shared location with UNC path like \\name\Shared

In such a scenario, the bat file typically starts the base from C:\Windows and throws a classNotFoundException. I can rather map the shared drive to a *Z:* drive or whatever but I do not want to do it.

I want the code to programatically detect and retrieve the content of Readme.txt in Addons folder irrespective of whether it is being executed on a local drive or on a shared drive. Is there a way to achieve this? Please help..

Thanks

Veekay

like image 377
Vamsi Emani Avatar asked Sep 20 '11 06:09

Vamsi Emani


People also ask

How do you read a file from a given location in Java?

You can use the Path class to get the path to the file since the Files class accepts the Path object of the file. You can use readAllBytes() to retrieve the data stored in the file to a byte array instead of a string array.

How do I access a shared drive in Java?

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.

How do I access a shared file in Windows?

Right click on the Computer icon on the desktop. From the drop down list, choose Map Network Drive. Pick a drive letter that you want to use to access the shared folder and then type in the UNC path to the folder. UNC path is just a special format for pointing to a folder on another computer.

How do I access a shared file from another computer?

Double-click the name of the computer from which the folder you want to open is being shared. Select a folder. Double-click the folder you want to open. Enter a username and password if prompted.


3 Answers

When using a file path in Java, make sure to escape all \ correctly when giving the full path name.

For example, if the file is on PC with IP (10.10.10.123) on a Shared folder called Addons then the full path will be:

File f = new File ("\\\\10.10.10.123\\Addons\\readme.txt");

Other than the full path, your code is throwing a ClassNotFound because you JAVA-CLASSPATH is not set properly.

like image 54
medopal Avatar answered Sep 30 '22 17:09

medopal


In your bat file %~dp0 expands to the location of the bat file. You need that in your classpath so that java can find the class, though I don't know if it will choke on UNC path. For example:

@echo off
echo %~dp0

would output

\\host\share\dir

EDIT: %dp0 will not work if there are spaces. This is what you need in your bat file:

@echo off
set p=%~dps0
echo %p%
java -classpath %p%\jarname classname
pause
like image 23
Miserable Variable Avatar answered Sep 30 '22 17:09

Miserable Variable


Two ways of doing.

1) Map the shared path to a local drive.

2) Else hard code the server path in new File('') as mentioned by Medopal.

Something like new File("").getAbsolutePath() might help me get the base Folder when executed on a local system. Likewise there is no such way to programmatically find out the working base when executed on a shared location.

like image 24
Vamsi Emani Avatar answered Sep 30 '22 18:09

Vamsi Emani