Quellcode durchsuchen

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 vor 10 Jahren
Ursprung
Commit
b8e539ef39

+ 1
- 1
src/com/dmdirc/commandline/CommandLineParser.java Datei anzeigen

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

+ 2
- 2
src/com/dmdirc/updater/components/ClientComponent.java Datei anzeigen

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

+ 28
- 3
src/com/dmdirc/util/resourcemanager/DMDircResourceManager.java Datei anzeigen

22
 
22
 
23
 package com.dmdirc.util.resourcemanager;
23
 package com.dmdirc.util.resourcemanager;
24
 
24
 
25
+import com.dmdirc.Main;
25
 import com.dmdirc.logger.ErrorLevel;
26
 import com.dmdirc.logger.ErrorLevel;
26
 import com.dmdirc.logger.Logger;
27
 import com.dmdirc.logger.Logger;
27
 
28
 
29
+import java.io.File;
28
 import java.io.UnsupportedEncodingException;
30
 import java.io.UnsupportedEncodingException;
29
 import java.net.MalformedURLException;
31
 import java.net.MalformedURLException;
30
 import java.net.URI;
32
 import java.net.URI;
31
 import java.net.URISyntaxException;
33
 import java.net.URISyntaxException;
32
 import java.net.URL;
34
 import java.net.URL;
33
 import java.net.URLDecoder;
35
 import java.net.URLDecoder;
36
+import java.security.CodeSource;
34
 
37
 
35
 /**
38
 /**
36
  * Provides utility methods for working with resources from the DMDirc distribution.
39
  * Provides utility methods for working with resources from the DMDirc distribution.
46
      *
49
      *
47
      * @return Current working directory
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
         return getWorkingDirectoryString(Thread.currentThread()
75
         return getWorkingDirectoryString(Thread.currentThread()
51
                 .getContextClassLoader().getResource("com/dmdirc/Main.class"));
76
                 .getContextClassLoader().getResource("com/dmdirc/Main.class"));
52
     }
77
     }
58
      *
83
      *
59
      * @return location or null
84
      * @return location or null
60
      */
85
      */
61
-    public static String getWorkingDirectoryString(final URL mainClassURL) {
86
+    static String getWorkingDirectoryString(final URL mainClassURL) {
62
         try {
87
         try {
63
             return URLDecoder.decode(getWorkingDirectoryURL(mainClassURL).getPath(), "UTF-8");
88
             return URLDecoder.decode(getWorkingDirectoryURL(mainClassURL).getPath(), "UTF-8");
64
         } catch (UnsupportedEncodingException ex) {
89
         } catch (UnsupportedEncodingException ex) {
74
      *
99
      *
75
      * @return URL location or null
100
      * @return URL location or null
76
      */
101
      */
77
-    public static URL getWorkingDirectoryURL(final URL mainClassURL) {
102
+    static URL getWorkingDirectoryURL(final URL mainClassURL) {
78
         if (isRunningFromJar(mainClassURL)) {
103
         if (isRunningFromJar(mainClassURL)) {
79
             return getJarURL(mainClassURL);
104
             return getJarURL(mainClassURL);
80
         } else {
105
         } else {

Laden…
Abbrechen
Speichern