I have a path in windows as:
C:\\Users\\Sneha\\.netbeans\\5.5.1\\tomcat55.properties
How can we convert the above path to Linux path. what does .netbeans and tomcat55.properties path means.
Any help would be appreciated.
you should read this webpage to understand difference between file system in linux and windows.
to convert that windows path to linux path, you must know that names of directories and files are case sensitive in linux,
then except using C:\\Users\\Sneha use this method in java to get current user path in any machine(windows, linux and etc.)
String user_dir = System.getProperty("user.home");
then in linux, path contains only forward slash /. not backward slash \.
so the path you said
C:\Users\Sneha\.netbeans\5.5.1\tomcat55.properties
will be
/home/Sneha/.netbeans/5.5.1/tomcat55.properties
also
.netbeans is a folder,
tomcat55.properties is a file.
Related
On Linux System.getenv("HOME") return absolute path /home/user, but on Windows return Users\user.
On Windows the home drive is specified separately in HOMEDRIVE variable. Concatenating it with HOMEPATH gives you the absolute path:
String home = System.getenv("HOMEDRIVE")+System.getenv("HOMEPATH");
You need to use System.getProperty("user.home") if you want something that works the same on multiple operating systems.
System.getenv is operating-system or context dependent - there is no guarantee whatsoever that System.getenv("HOME") returns anything in particular on a given operating system, it's just luck that what you get on Unix is anything similar to what you get on Windows.
From the Javadoc for System.getenv:
An environment variable is a system-dependent external named value.
For System.getProperty, there is a list of properties that you can get in a system-independent way:
https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties()
One of them is user.home: "User's home directory"
I have an application written in java in which forward slash to specify the file and directory path . The file and directory can access when the application run on linux. But when it run on windows it says an error that the specified path is incorrect.How to uniquely specify the path of the file.
In java iam using this command:
public static final String WD ="/qark-master/qark/qarkMain.py";
public static final String MANIFESTPATH="/apktool/AndroidManifest.xml";
Please help me here!
As mentioned by Jim Garrison, forward slash works in Windows as well as in Unix.
Problem is with drive letter or root directory. When in Windows path defined from root like /qark-master it is a root directory of the current drive.
But... use absolute path in the code either in Windows with drive letter or from root in Linux is not a good idea. Much better is to use relative path either from current running directory or special environment variable.
Then you can use forward slash and do not care about path separator.
From other hands - there is a System property in JVM called "file.separator" and it is possible to construct a path with it according to OS. Of course problem for absolute path with drive letter for Windows is there anyway.
While Java will happily use forward slashes in both Windows and Linux, the requirement for a drive letter prefix in Windows makes it impossible to use the same absolute paths in both systems.
What you will need to do is use a properties file to configure OS-dependent parameters, such as file locations, and have a different version of the properties file on each system.
Note that it is very bad practice to hard code external resource references (i.e. file paths) in your Java code. Relative references are OK but they must be relative to some base location that is provided at runtime and not compiled into the executable.
You need to escape characters for escape sequences. More details here - Escape Characters
In Windows, You need to defined a escape character for file sepeator with backslash - as below.
String filePath = "C:\\Users\\b21677\\DFS.docx";
In Linux, You shall define as is
public static final String WD ="/qark-master/qark/qarkMain.py";
This works fine when i used file.separator.
public static final String QWD = File.separator +"qark-master" + File.separator +"qark" + File.separator +"qarkMain.py";
public static final String MANIFESTPATH=File.separator +"apktool"+ File.separator +"AndroidManifest.xml";
In a java method i am loading a path name into a string variable, not knowing whether it is a Unix or a Windows path name.
How can i programmatically check whether it is a Unix or a Windows path?
if path.contains(":") execute a Windows-related function
else execute a Unix-related function
won't work, because Unix path names can contain a ":" as far as i know. Same with "/" for Unix , because Windows path names can contain a "/" .
So how could i do this best?
EDIT: I am loading directory names for remote machines, so i doubt i can use Use System.getProperty("os.name").
i was thinking about using Regex, but since i have no idea about regex in Java i have not considered that yet.
IMHO, using path names to determine OS platform is not the best idea.
Use System.getProperty("os.name")
and have a look here:
How do I programmatically determine operating system in Java?
I want to run jar file at windows startup. I made an entry at registry successfully.
The jar file is running successfully at system startup.
But the problem is, the file running successfully with absolute path.
The sample path is as follows:
C:\Users\...\Desktop\Jars\myJar.jar
But I want to run this file with relative path. I developed the application in eclipse, and get the path of "myJar2.jar" and executes within myJar.jar file.
I get the path of "myJar2.jar" file by calling getCanonicalPath() method. In registry , it will display the path as follows:
D:\Users\...\ProjectName/Jars/myJar.jar //path by using getCanonicalPath() method , stored in registry
How to run "marJar.jar" at system startup with above relative path.
Thanks in advance...
D:\Users\...\ProjectName/Jars/myJar.jar this is no vaild path, since it contains both '/' & '\'. eliminate either of those.
to correct '\' or '/' in path use this:
String path = <some_path_here>;
path = path.replaceAll("\", File.pathSeparator);
path = path.replaceAll("/", File.pathSeparator);
I'm working on restoration application where the restoration paths are either windows paths or unix paths (I don't know which straight up) and then I need to map them to an appropriate path in the current OS.
Obviously Windows ==> Windows, *nix ==> *nix are straight forward. However when the original paths are from Windows moving them to *nix is problematic since *nix would interpret these paths as a file name (for example C:\folderA\fileB.txt is mapped to file in *nix and not as a path of /folderA/fileB.txt)
What I want to do is parse the paths in advance, determine if they are windows/*nix paths and then treat them accordingly.
Any suggestions?
Jonathan
The java.io.File class works fine with slash separated paths on both platforms.
You can also parse the paths for the File.separator values, which is based on the platform the program is running on.
Since the Windows APIs treat backslash \ and slash / identically as path separators, and since sane people on Unix don't embed backslashes in file names, you could simply convert backslashes to slashes. That would leave you with just drive letters (C: etc) at the start of the path to deal with.