determine whether path is local or network path - java

Is there a way in Java to check whether given directory path is on local drive (path like "C:\data\temp") or on shared network (path like "\\<remote_m/c_ip>\share")?
Please note that, in windows environment network path can be mapped as drive. So in this case, "Z:\temp" may be actually on network. And similarly we can share local folder and access it as: \\<local m/c ip>\share, even though it is actually on local drive.

You could run, using java: "cmd fsutil fsinfo drivetype {drive letter}" The output will tell you if it's a network drive or not.
Check Run cmd commands through java to see how to use CMD to run commands.

Related

Execute program on user's PATH within Java

Within a Java 1.8 program, how can I execute a program on the user's PATH? For simplicity let's assume that only *nix systems have to be supported. What I want to do is something like:
Runtime.getRuntime().exec("myProgram")
where myProgram is somewhere on the user's PATH.
Runtime.getRuntime().exec(cmd) can only "see" programs on the system's path. For example in my case (mac os) /usr/bin:/bin:/usr/sbin:/sbin, but what if myProgram is in /usr/local/bin or ~/bin?
I tried to get the directories on the user's PATH by parsing the output of Runtime.getRuntime().exec("echo $PATH"), but I get back only the string "$PATH" itself, not the actual content of the PATH variable.
EDIT Executing System.getenv() gives me only PATH=/usr/bin:/bin:/usr/sbin:/sbin, i.e. not the user's PATH.
If you want to get the actual content of the PATH variable you can use:
System.getenv():
Good luck!
String userPath = System.getenv("PATH");
If it returns a null string then try exporting that $PATH variable before running your java program.

What is the recommended way of accessing a network share folder (located in Windows or Linux) in java

All, Forgive me I am not familiar with the Linux.
I am trying to read all the files of a network share folder which is located in either Windows or Linux system.
Currently I just made it work for the case of Windows by below code.
networkShareFolder="\\\\10.50.90.18\\ITS Tool\\xml\\";//It is a windows Network share path.
File[] files = new File(networkShareFolder).listFiles();
But When I deploy my application to the Linux system and run it. It just told me can not get any files from the specified networkShareFolder;
So I tried to type the path \\10.50.90.18 in the File explorer of Linux like what I did in the windows. To see if the path can be reached from the Linux system. But it just told me Can't locate the \\10.50.90.18. But I am sure the IP can be ping from the Linux.
So my questions are
Why \\10.50.90.18 can't be accessed in Linux .But can be accessed in Windows. (I am sure their IP are all 10.50.90.*)
What is the best way to access the network share folder from windows or linux ?
Thanks.
Remote Mount with FUSE
It's possible to mount a remote filesystem (generally including SMB/CIFS) with FUSE and samba. That might look something like (assuming you have a mountpoint /windows)
# export USER=efrisch
# export WORKGRP=mygrp
# smbmount //10.50.90.18/ /windows –o username=$USER,workgroup=$WORKGRP
Then you could access your directory (transparently) with
new File("/windows/ITS Tool/xml")
Pure Java Solution (with JCIFS)
JCIFS provides SmbFile and that provides listFiles() allowing something like
SmbFile[] files = new SmbFile("smb://10.50.90.18/ITS Tool/xml/").listFiles();
The linked documentation for SmbFile does give the full format as
smb://[[[domain;]username[:password]#]server[:port]/[[share/[dir/]file]]][?param=value[param2=value2[...]]]
and it also notes that all SMB URLs that represent workgroups, servers, shares, or directories require a trailing slash '/'.

java.exe is not recognized as an internal or external command

I have already read some posts but I cant solve my problem yet.
I am working on a remote desktop and windows server 2008. In the shared disk E: I put some batch file. This batch files call a new batch file from server which runs java script. And now I am taking the this message.
java.exe is not recognized as an internal or external command, operable program or batch file.
I try to set the environment like :
First setup the JRE7 to disk E
Second create new user variable which name is JAVA_HOME and which path is my JRE path E:\Tool\BatFiles
But I am still taking this error. Where should I do wrong ?
Setting JAVA_HOME is a good step, and with it you should be able to run Java as follows
%JAVA_HOME%/java myProgram arg0
If you don't want to include %JAVA_HOME in your command, you will have to include it in your PATH. Windows checks it's PATH for bin scripts every time a command is called. A typical Java installation does this for you.
You can edit your PATH to include ;%JAVA_HOME% at the end. Restart your command prompt for changes to take effect.
Edit 1
Be careful when editting your PATH however! Windows depends on it to function in many aspects. You can expect explorer to stop working. Make sure before altering your PATH variable, that you back it up somewhere. Just in case.

Java create directory using File for Unix / Windows

I need to create a directory on unix machine. I think the below code will work fine on unix machine but fails while testing it on local windows machine. Where does this directory get created on my local machine ?
String xmlDir = "/home/data/logs"
File xmlDirectory = new File(xmlDir);
xmlDirectory.mkdir();
I tried below directory path and it worked fine on windows machine. But i had to use the mkdirs() instead of mkdir() method which needs to be used for unix directory creation?
String xmlDir = "C:\\home\\data\\logs"
File xmlDirectory = new File(xmlDir);
xmlDirectory.mkdirs();
How can I make it work locally as well as n unix machine ? Is there a better way for File and Directory creation ?
--Thanks--
You should use the System user.home property which will return the user's home directory in a system independent manner, for example...
File home = new File(System.getProperty("user.home"));
mkdir will only create the last element in the path, where as mkdirs will create all the elements that do not exist. Using mkdirs is probably a slightly better idea as it ensures (where permissions allow) that all elements in the path will be created if they do not exist
You have already hit on the answer: Just use mkdirs(). It is not platform dependent. However, if you include platform dependent nomenclature, then you'll run into trouble when moving the code from one environment to another. Just be sure to use platform independent code, or at the very least, check for the OS before doing so via System.getProperty("os.name");

Open a PDF file on a remote machine with local app in Java?

When I run code like this in Mac os:
Runtime.getRuntime.exec("open testFile.pdf");
the Mac OS will run Acrobat to open the local PDF file.
How can I do it, when the file is on a remote machine?
\\remoteHost\share\testFile.pdf
I try to do that like this:
Runtime.getRuntime.exec("open \\\\remoteHost\\share\\testFile.pdf");
but I failed.
Thanks!
Runtime.getRuntime().exec(String) just passes the String to the OS for it to handle it. So, if you want to run the command
open \\remoteHost\share\testFile.pdf
You have to pass it to exec(). Remember to escape the \ (replace every \ by \\)
Runtime.getRuntime().exec("open \\\\remoteHost\\share\\testFile.pdf");
Of course, the user running the program should have permissions in the remote machine. If you need to set another user, use the open program command line parameters.
I don't think it's possible to directly specify the path of an SMB share while using the open command. If you want to use open, you would have to mount that share as a local directory. See http://osxdaily.com/2009/09/24/access-and-mount-an-smb-share-via-command-line/ for an explanation.
A solution like that would be pretty fragile though. If you want a more reliable solution, I'd suggest using JCIFS (http://jcifs.samba.org/) to download the file locally and then use
Desktop.getDesktop().open(pdfFile);
to open the downloaded file.

Categories

Resources