Browse Source

Rename confusing method name and add correctly functioning one.

This fixes portable mode when running from a jar only.

Change-Id: I8272e2ff2fe1c6d88d6f6c41c859eb2d2124e04c
Issue: CLIENT-496
Reviewed-on: http://gerrit.dmdirc.com/3402
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.8.1rc2^0
Greg Holmes 10 years ago
parent
commit
b8e539ef39

+ 1
- 1
src/com/dmdirc/commandline/CommandLineParser.java View File

@@ -233,7 +233,7 @@ public class CommandLineParser {
233 233
                 launcherVersion = param;
234 234
                 break;
235 235
             case 'p':
236
-                doDirectory(DMDircResourceManager.getCurrentWorkingDirectory());
236
+                doDirectory(DMDircResourceManager.getWorkingDirectory());
237 237
                 break;
238 238
             case 'r':
239 239
                 disablereporting = true;

+ 2
- 2
src/com/dmdirc/updater/components/ClientComponent.java View File

@@ -100,7 +100,7 @@ public class ClientComponent implements UpdateComponent {
100 100
                         + "not be installed automatically.\n\n"
101 101
                         + "To install this update manually, please replace the\n"
102 102
                         + "existing DMDirc.jar file, located at:\n"
103
-                        + " " + DMDircResourceManager.getCurrentWorkingDirectory() + "\n"
103
+                        + " " + DMDircResourceManager.getApplicationDirectory() + "\n"
104 104
                         + "with the following file:\n"
105 105
                         + "  " + targetFile.getAbsolutePath();
106 106
             } else {
@@ -111,7 +111,7 @@ public class ClientComponent implements UpdateComponent {
111 111
                         + "new DMDirc.jar file, located at:\n"
112 112
                         + " " + targetFile.getAbsolutePath() + "\n"
113 113
                         + "over your existing DMDirc install located in:\n"
114
-                        + "  " + DMDircResourceManager.getCurrentWorkingDirectory();
114
+                        + "  " + DMDircResourceManager.getApplicationDirectory();
115 115
             }
116 116
         }
117 117
         return "";

+ 28
- 3
src/com/dmdirc/util/resourcemanager/DMDircResourceManager.java View File

@@ -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 {

Loading…
Cancel
Save