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");
Related
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.
I am working on a simple program in java, which I use to read and write a few things in one SQLITE database, located within the directory I have the jar file. To access the database and a settings file, I use the
String wd = System.getProperty("user.dir");
String dbName = "jdbc:sqlite:"+wd+"\\"+sqliteDataNae;
c = DriverManager.getConnection(dbName);
Long story short,
I have made a jar file wrapping all libraries in and tested it in Windows where everything works fine. I get my data stored in my database. Not exception at all!
Then I transfer all my files in a rar file to an ubuntu (14) hyperv virtual machine.
I use the terminal to go to my directory where I have unzipped everything, and run:
java -jar myJar.jar
The script starts running, I get my println and the setting are all loaded (hence no issue reading the settings txt file - located in the same directory as the database = no directory issue),
The problem is that I get an sqlite exception [SQLITE_ERROR] SQL error or missing database (no such table: User).
However, I open my database (using SQLite Database Browser) and the table exists.
Is there any logical explanation for this? and is there any solution?
My guess is since Linux uses forward slashes for path separators you'll need to replace '\\' with '/'. However to make life easier just use File.separator instead as it automatically gives you the default separator for the current OS.
You should use File.pathSeparator it's used to separate individual file paths (Every OS uses different kind of Path variable).
For windows, it’s ‘\’ and for unix it’s ‘/’.
e.g: File file = new File("abc"+File.separator+"def.txt");
//on Unix system prints: abc/def.txt
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 '/'.
I have Java Code that launch process of WinSCP tool and connects to a Unix machine and then call a xxxx.exe located on the Unix machine.
The problem is that xxxx.exe accepts a parameter of a type File. So I need to upload this to the remote machine and then passed to the xxxx.exe.... that is failing
and I'm trying to avoid the temporary folders as possible.
small Code
Process p = Runtime.getRuntime().exec("rTool\\WinSCP.com /script=folder\\code.txt < C:\\FILESTOUPLOADS\\upload1.txt" );
The login information goes in code.txt as supported by WinSCP.com
file redirection (i.e. the "<" symbol) is handled my the command processor, which Runtime.exec() does not use. As mentioned in comments already, first use the String[] version of exec so that you don't have issues with command parsing. second, you need to invoke the command processor to handle the file redirection (e.g. using "cmd.exe /k"), or handle it yourself in java.
Why not use ProcessBuilder to change working directory and set path of the file from that directory
public ProcessBuilder directory(File directory)Sets this process builder's working directory. Subprocesses subsequently started by this object's start() method will use this as their working directory. The argument may be null -- this means to use the working directory of the current Java process, usually the directory named by the system property user.dir, as the working directory of the child process.
Parameters:
directory - The new working directory
Returns:
This process builder
I'm not sure if it is best practice but I add MySQL-connector jar to the extensions directory of my Java install directory to I can easily connect to MySQL databases.
I also set environment variables to point to various directories so that I can develop on different machines and only define environment variables locally and code doesn't have to be modified for file paths.
In either case of the above I find that unless I reboot my computer java does not recogise either. What happens during a reboot to Java? Is some config file updates by a java process? Can you update this without having to reboot?
To test this I have created a new environment variable on both Mac (adding to .MacOS/environment.plist), Linux (Ubuntu 12.04) and windows 7 (via control panel). I then used System.getenv("TestVar"); which returns null. Running set from the command line shows it exists though. After a reboot System.getenv("TestVar"); returns the expected value.
Ultimately your goal is to include jar files in CLASSPATH . its up to you how include jars in classpath but this is not good practice to put jars inside extensions directory . While running your program modify CLASSPATH value .
java -cp jar1:jar2:jar3:dir1:. HelloWorld
java -classpathjar1:jar2:jar3:dir1:. HelloWorld
As far as setting environment variables goes the on Ubuntu a log out is required
https://superuser.com/questions/339617/how-to-reload-etc-environment-without-rebooting