Skip to content

Commit ce89f20

Browse files
authored
Merge pull request #16 from scito/master
Fix #15: Convert URL directly into a File
2 parents 0ef725b + f596cc3 commit ce89f20

1 file changed

Lines changed: 24 additions & 30 deletions

File tree

src/matlabcontrol/Configuration.java

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.File;
99
import java.io.IOException;
1010
import java.lang.reflect.Method;
11+
import java.net.URISyntaxException;
1112
import java.net.URL;
1213
import java.security.CodeSource;
1314
import java.security.ProtectionDomain;
@@ -201,49 +202,42 @@ static String getSupportCodeLocation() throws MatlabConnectionException {
201202
URL url = codeSource.getLocation();
202203
if (url != null) {
203204
//Convert from url to absolute path
204-
//path could be null
205-
String path = url.getFile();
206-
if (path != null) {
207-
try {
208-
File file = new File(path).getCanonicalFile();
209-
if (file.exists()) {
210-
return file.getAbsolutePath();
211-
} else {
212-
ClassLoader loader = Configuration.class.getClassLoader();
213-
throw new MatlabConnectionException("Support code location was determined improperly." +
214-
" Location does not exist.\n" +
215-
"Location determined as: " + file.getAbsolutePath() + "\n" +
216-
"Path: " + path + "\n" +
217-
"URL Location: " + url + "\n" +
218-
"Code Source: " + codeSource + "\n" +
219-
"Protection Domain: " + domain + "\n" +
220-
"Class Loader: " + loader +
221-
(loader == null ? "" : "\nClass Loader Class: " + loader.getClass()));
222-
}
223-
}
224-
//Unable to resolve canconical path
225-
catch (IOException e) {
205+
try {
206+
File file = new File(url.toURI()).getCanonicalFile();
207+
if (file.exists()) {
208+
return file.getAbsolutePath();
209+
} else {
226210
ClassLoader loader = Configuration.class.getClassLoader();
227-
throw new MatlabConnectionException("Support code location could not be determined. " +
228-
"Could not resolve canonical path.\n" +
229-
"Path: " + path + "\n" +
211+
throw new MatlabConnectionException("Support code location was determined improperly." +
212+
" Location does not exist.\n" +
213+
"Location determined as: " + file.getAbsolutePath() + "\n" +
214+
"File: " + file + "\n" +
230215
"URL Location: " + url + "\n" +
231216
"Code Source: " + codeSource + "\n" +
232217
"Protection Domain: " + domain + "\n" +
233218
"Class Loader: " + loader +
234-
(loader == null ? "" : "\nClass Loader Class: " + loader.getClass()), e);
219+
(loader == null ? "" : "\nClass Loader Class: " + loader.getClass()));
235220
}
236221
}
237-
//path was null
238-
else {
222+
//Unable to resolve canconical path
223+
catch (IOException e) {
224+
ClassLoader loader = Configuration.class.getClassLoader();
225+
throw new MatlabConnectionException("Support code location could not be determined. " +
226+
"Could not resolve canonical path.\n" +
227+
"URL Location: " + url + "\n" +
228+
"Code Source: " + codeSource + "\n" +
229+
"Protection Domain: " + domain + "\n" +
230+
"Class Loader: " + loader +
231+
(loader == null ? "" : "\nClass Loader Class: " + loader.getClass()), e);
232+
} catch (URISyntaxException e) {
239233
ClassLoader loader = Configuration.class.getClassLoader();
240234
throw new MatlabConnectionException("Support code location could not be determined. " +
241-
"Could not get path from URI location.\n" +
235+
"Could not convert URI location to file path.\n" +
242236
"URL Location: " + url + "\n" +
243237
"Code Source: " + codeSource + "\n" +
244238
"Protection Domain: " + domain + "\n" +
245239
"Class Loader: " + loader +
246-
(loader == null ? "" : "\nClass Loader Class: " + loader.getClass()));
240+
(loader == null ? "" : "\nClass Loader Class: " + loader.getClass()), e);
247241
}
248242
}
249243
//url was null

0 commit comments

Comments
 (0)