ProtectionDomain domain = Main.class.getProtectionDomain();
CodeSource source = null;
URL result = null;
if (domain != null)
source = domain.getCodeSource();
if (source == null || domain == null) {
if (debug)
System.out.println("CodeSource location is null. Defaulting the install location to file:startup.jar"); //$NON-NLS-1$
try {
result = new URL("file:startup.jar"); //$NON-NLS-1$
} catch (MalformedURLException e2) {
//Ignore
}
}
if (source != null)
result = source.getLocation();
String path = decode(result.getFile());
// normalize to not have leading / so we can check the form
File file = new File(path);
path = file.toString().replace('\\', '/');
// TODO need a better test for windows
// If on Windows then canonicalize the drive letter to be lowercase.
// remember that there may be UNC paths
if (File.separatorChar == '\\')
if (Character.isUpperCase(path.charAt(0))) {
char[] chars = path.toCharArray();
chars[0] = Character.toLowerCase(chars[0]);
path = new String(chars);
}
if (path.toLowerCase().endsWith(".jar")) //$NON-NLS-1$
path = path.substring(0, path.lastIndexOf("/") + 1); //$NON-NLS-1$
if (path.toLowerCase().endsWith("/plugins/")) //$NON-NLS-1$
path = path.substring(0, path.length() - "/plugins/".length()); //$NON-NLS-1$
try {
try {
// create a file URL (via File) to normalize the form (e.g., put
// the leading / on if necessary)
path = new File(path).toURL().getFile();
} catch (MalformedURLException e1) {
// will never happen. The path is straight from a URL.
}
installLocation = new URL(result.getProtocol(), result.getHost(), result.getPort(), path);
System.getProperties().put(PROP_INSTALL_AREA, installLocation.toExternalForm());
} catch (MalformedURLException e) {
// TODO Very unlikely case. log here.
}