/** * Extracts the root directory for a class file. * * <p>Class Path = directory + package + class name + .class (in every form, with jars, without, * etc.) Full Class Name = package + class name + .class * * @param classPath * @return */ static String getClassRootDirectory(String classPath, String fullClassName) { System.out.println("Class Path : " + classPath); classPath = UrlUtils.decode(classPath); // Find ending trim location... if (classPath.contains("!")) { // A jar file (in format 'directory/jarfile.jar!package/className.class'): classPath = StringUtils.toLeftOf(classPath, "!"); // Remove '!package/classname.class' classPath = StringUtils.toLeftOfLast(classPath, "/"); // Remove jar name. } else { // A class file ('appdir/package/className.class'): classPath = classPath.substring( 0, classPath.length() - fullClassName.length() - 7); // -1 for '.' in front of class name, -6 for .class } if (classPath.contains("file:")) { classPath = classPath.substring(5); } return classPath; }
/** * Returns the application directory. Important: The method getAppDir (Class) has to be called * first. * * @return */ public static String getAppDirectory() { // Return the directory the application executable / source is located. ////////////////////////////////////////////////////////////////// // Declarations: ////////////////////////////////////////////////////////////////// String className = null; URL classUrl = null; String directoryName = null; int trimStart = 0; int trimEnd = 0; OSProperties osProps; ////////////////////////////////////////////////////////////////// // Code: ////////////////////////////////////////////////////////////////// osProps = OSPropertiesFactory.getInstance(); className = getApplicationClass().getSimpleName() + ".class"; classUrl = getApplicationClass().getResource(className); directoryName = classUrl.getPath(); // System.out.println ("class path: " + directoryName); // System.out.println ("class name: " + getApplicationClass().getName()); // Find starting trim location... if (osProps.isOSWindows()) { if (directoryName.contains(":")) { // Contains URL Descriptor. Remove it. trimStart = directoryName.lastIndexOf(':') - 1; } else { // Remove leading '/'. trimStart = 1; } } // Find ending trim location... if (directoryName.contains("!")) { // Is in a jar file. // Find '!' location in name... trimEnd = directoryName.indexOf('!'); // Find previous '/'. trimEnd = directoryName.lastIndexOf('/', trimEnd - 1); } else { // should be a file. // Trim to class name. // trimEnd = directoryName.length() - getApplicationClass().getName().length() - 7; trimEnd = directoryName.length() - 7; } // Trim and format directory... /* * Debug - Delete if no problems found. * System.out.println (directoryName); System.out.println (getApplicationClass().getName()); System.out.println (trimStart); System.out.println (trimEnd); */ directoryName = directoryName.substring(trimStart, trimEnd); directoryName = UrlUtils.decode(directoryName); // Change the correct slashes... directoryName = osProps.formatPath(directoryName); return directoryName; }