|
@@ -22,15 +22,18 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.util.resourcemanager;
|
24
|
24
|
|
|
25
|
+import com.dmdirc.Main;
|
25
|
26
|
import com.dmdirc.logger.ErrorLevel;
|
26
|
27
|
import com.dmdirc.logger.Logger;
|
27
|
28
|
|
|
29
|
+import java.io.File;
|
28
|
30
|
import java.io.UnsupportedEncodingException;
|
29
|
31
|
import java.net.MalformedURLException;
|
30
|
32
|
import java.net.URI;
|
31
|
33
|
import java.net.URISyntaxException;
|
32
|
34
|
import java.net.URL;
|
33
|
35
|
import java.net.URLDecoder;
|
|
36
|
+import java.security.CodeSource;
|
34
|
37
|
|
35
|
38
|
/**
|
36
|
39
|
* Provides utility methods for working with resources from the DMDirc distribution.
|
|
@@ -46,7 +49,29 @@ public class DMDircResourceManager {
|
46
|
49
|
*
|
47
|
50
|
* @return Current working directory
|
48
|
51
|
*/
|
49
|
|
- public static synchronized String getCurrentWorkingDirectory() {
|
|
52
|
+ public static synchronized String getWorkingDirectory() {
|
|
53
|
+ final CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
|
|
54
|
+ try {
|
|
55
|
+ final File jarFile = new File(codeSource.getLocation().toURI().getPath());
|
|
56
|
+ String jarDir;
|
|
57
|
+ if (isRunningFromJar()) {
|
|
58
|
+ jarDir = jarFile.getParentFile().getPath();
|
|
59
|
+ } else {
|
|
60
|
+ jarDir = jarFile.getPath();
|
|
61
|
+ }
|
|
62
|
+ return URLDecoder.decode(jarDir, "UTF-8");
|
|
63
|
+ } catch (URISyntaxException | UnsupportedEncodingException ex) {
|
|
64
|
+ Logger.appError(ErrorLevel.FATAL, "Unable to find working directory", ex);
|
|
65
|
+ return null;
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ /**
|
|
70
|
+ * Returns the working directory for the application or the jar name.
|
|
71
|
+ *
|
|
72
|
+ * @return Current working directory
|
|
73
|
+ */
|
|
74
|
+ public static synchronized String getApplicationDirectory() {
|
50
|
75
|
return getWorkingDirectoryString(Thread.currentThread()
|
51
|
76
|
.getContextClassLoader().getResource("com/dmdirc/Main.class"));
|
52
|
77
|
}
|
|
@@ -58,7 +83,7 @@ public class DMDircResourceManager {
|
58
|
83
|
*
|
59
|
84
|
* @return location or null
|
60
|
85
|
*/
|
61
|
|
- public static String getWorkingDirectoryString(final URL mainClassURL) {
|
|
86
|
+ static String getWorkingDirectoryString(final URL mainClassURL) {
|
62
|
87
|
try {
|
63
|
88
|
return URLDecoder.decode(getWorkingDirectoryURL(mainClassURL).getPath(), "UTF-8");
|
64
|
89
|
} catch (UnsupportedEncodingException ex) {
|
|
@@ -74,7 +99,7 @@ public class DMDircResourceManager {
|
74
|
99
|
*
|
75
|
100
|
* @return URL location or null
|
76
|
101
|
*/
|
77
|
|
- public static URL getWorkingDirectoryURL(final URL mainClassURL) {
|
|
102
|
+ static URL getWorkingDirectoryURL(final URL mainClassURL) {
|
78
|
103
|
if (isRunningFromJar(mainClassURL)) {
|
79
|
104
|
return getJarURL(mainClassURL);
|
80
|
105
|
} else {
|