For PATH
and CLASSPATH
variables, you need to discover the conventional separator for them.
PATH
String path = System.getEnv("PATH");
// See http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
String[] files = path.split(System.getProperty("path.separator"));
List<String> unescapedFiles = new ArrayList<>();
for (String file: files) {
// Exercise for the reader
String unquotedFile = ...;
String javaSlashedFile = ...;
unescapedFiles.add(javaSlashedFile);
}
return unescapedFiles;
The loop should take file names like "C:\Program Files\perl\perl.exe"
and convert them to C:/Program Files/perl/perl.exe
.
CLASSPATH
There is a more reliable alternative for the class path. Also listed on the same Java Tutorial page is the system property "java.class.path". This is more reliable than the environment variable because it takes into account java -cp *path*
invocations and JAR files with Class-Path
manifests. So, replace the line
String path = System.getEnv("PATH");
with
String classPath = System.getProperty("java.class.path");