Reputation: 109
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!
Upvotes: 6
Views: 5126
Reputation: 109
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";
Upvotes: 0
Reputation: 4120
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.
Upvotes: 4
Reputation: 2718
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";
Upvotes: 0
Reputation: 86764
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.
Upvotes: 2