How to differentiate between window / *nix paths - java

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.

Related

Backslash vs. Forward slash in Java

Why does there need to be a forward slash in the path to the class file when using Windows Command Prompt?
Input:
C:\javafun>java --module-path C:\javafun\mod --module jdojo.intro/com.jdojo.intro.Welcome
Output:
Welcome to Java 17!
I've read that when running .jar files there needs to be a forward slash, but I've created a modular .jar file in C:\javafun\lib directory. Here I am just trying to go straight to the .class file in the C:\javafun\mod\jdojo.intro\com\jdojo\intro\Welcome.class
Why when I change the below to a backslash when using Windows I get the following error? This is confusing to know when to use specific slashes when dealing with file locations, packages or directories.
Input:
C:\javafun>java --module-path C:\javafun\mod --module jdojo.intro\com.jdojo.intro.Welcome
Output:
Error occurred during initialization of boot layer
java.lang.module.FindException: Module jdojo.intro\com.jdojo.intro.Welcome not found
In Windows, file paths need to use the backslash as a separator. However, this is not a file path:
--module jdojo.intro\com.jdojo.intro.Welcome
The forward slash is a designated separator, on all platforms, between the module name and the class name.
The fact that, on non-Windows systems, file paths happen to use the forward slash character as a directory separator, but it’s a different character in Windows, does not mean that all non-file uses of the forward slash will also be different on Windows.
In fact, the documentation for the java command explicitly states that the character between the module name and the class name must be a forward slash:
To launch the main class in a module:
java [options] -m module[/mainclass] [args ...]
or
java [options] --module module[/mainclass] [args ...]
No matter what platform you’re running on, the character between a module name and a class name is a forward slash, because it’s not a file name and therefore is not platform dependent.

How to specify the path to access a file in both windows and linux uniquely using slash seperator

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";

Java: Convert windows path to Unix path

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.

how programmatically distinguish between Unix and Windows directory

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?

Command-line options working in windows, but not Linux

I have a .bat file on Windows/shell script on Linux that starts a large Java application from the command line. It configures the classpath, environment variables, etc.
At one point, it uses RMID to configure a bunch of services which will run in their own JVMs. The problem is that it won't allow me to specify multiple JARs for the codebase property on Linux. It's allowing me to do so on Windows just fine, but I think my syntax/styling must be wrong for the .sh script and am hoping a more experienced Linux user could have some tip. On Windows, the working line looks like this:
SET RMID_OPTIONS=%RMID_VM%
-J-DINSTALL_DIR=%CONFIG_PATH%
-C-DINSTALL_DIR=%CONFIG_PATH%
-J-DINSTALL_DIR_LOCAL=%HOME_DIR%
-C-DINSTALL_DIR_LOCAL=%HOME_DIR%
-J-Djava.security.policy=%PL_HOME%\windows\system.policy
-C-Djava.rmi.server.codebase=
"file:/%HOME_DIR%\jar1.jar file:/%HOME_DIR%\jar2.jar"
-J-Djava.rmi.server.codebase=
"file:/%HOME_DIR%\jar1.jar file:/%HOME_DIR%\jar2.jar"
// more stuff here
The only important lines are the ones setting the rmi.server.codebase property. The above works 100% fine, however, when trying to set multiple JARs in the codebase in Linux, it causes a general failure and the whole RMID command is not executed. My shell script looks like the following:
export RMID_OPTIONS="${RMID_VM}
-J-DINSTALL_DIR=${CONFIG_PATH}
-C-DINSTALL_DIR=${CONFIG_PATH}
-J-DINSTALL_DIR_LOCAL=${HOME_DIR}
-C-DINSTALL_DIR_LOCAL=${HOME_DIR}
-J-Djava.security.policy=${PL_HOME}/linux/system.policy
-C-Djava.rmi.server.codebase=
""file:/${HOME_DIR}/jar1.jar file:/${PL_HOME_LOCAL}/jar2.jar""
-J-Djava.rmi.server.codebase=
""file:/${HOME_DIR}/jar1.jar file:/${PL_HOME_LOCAL}/jar2.jar""
// more stuff here
"
The shell script itself works perfectly fine if only one JAR is specified, but any more and I get a general failure. Any suggestions on what I'm doing wrong? I'm open to try new things to fix this as all my attempts so far have been fruitless.
Under Linux, escaping quotes is done differently. You are attempting to use the Windows specific syntax, which will result in the jar files being passed as separate arguments, instead of a single one, as it should be.
Instead of "" to produce a quote inside quotes, you have to use \" in Linux:
export RMID_OPTIONS="... -C-Djava.rmi.server.codebase=\"file:/${HOME_DIR}/jar1.jar file:/${PL_HOME_LOCAL}/jar2.jar\" ..."
Aside from that, I'm not sure that the file:/ syntax is correct. It's probably either file:// or the absolute file path without anything preceding it, but you'll have to try it out.
You're doing this wrong. You don't need to start rmid with arguments and system properties at all. All that stuff should be specified when you register the ActivationGroup(s) you're going to use, in your activation setup program. That in turn means that all command-line problems should just disappear.

Categories