RobotCore
1.0
WPILib port to raspberry Pi
Toggle main menu visibility
Loading...
Searching...
No Matches
ClasspathUtils.java
1
/*
2
* Copyright (c) 2013 Creative Sphere Limited.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
6
* http://www.apache.org/licenses/LICENSE-2.0
7
*
8
* Unless required by applicable law or agreed to in writing,
9
* software distributed under the License is distributed on an "AS IS" BASIS,
10
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
* See the License for the specific language governing permissions and
12
* limitations under the License.
13
*
14
* Contributors:
15
*
16
* Creative Sphere - initial API and implementation
17
*
18
*/
19
package
org.ah.java.remotevmlauncher;
20
21
import
java.io.File;
22
import
java.io.IOException;
23
import
java.net.URL;
24
import
java.net.URLClassLoader;
25
import
java.net.URLDecoder;
26
32
public
class
ClasspathUtils
{
33
34
public
static
String obtainInitialPath()
throws
IOException {
35
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
36
if
(contextClassLoader instanceof URLClassLoader) {
37
URL[] urls = ((URLClassLoader)contextClassLoader).getURLs();
38
StringBuilder sb =
new
StringBuilder();
39
for
(URL url : urls) {
40
if
(
"file"
.equals(url.getProtocol())) {
41
if
(sb.length() > 0) {
42
sb.append(File.pathSeparator);
43
}
44
sb.append(url.getPath().replace(
"\\"
,
"/"
));
45
}
else
{
46
throw
new
RuntimeException(
"Cannot handle protocol "
+ url.getProtocol());
47
}
48
}
49
return
sb.toString();
50
}
else
{
51
throw
new
RuntimeException(
"Class is loaded from a file, but classloader is not URLClassLoader; "
+ contextClassLoader.getClass().getName());
52
// It is probably wrong but that's the only fallback we have...
53
// return new File(root.getFile()).getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getAbsolutePath();
54
}
55
}
56
57
public
static
String obtainPathOfClass(Class<?> cls)
throws
IOException {
58
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
59
String className = cls.getName().replace(
'.'
,
'/'
) +
".class"
;
60
URL root = contextClassLoader.getResource(className);
61
if
(
"jar"
.equals(root.getProtocol())) {
62
String file = URLDecoder.decode(root.getFile(),
"UTF-8"
).replace(
"\\"
,
"/"
);
63
URL url =
new
URL(file);
64
if
(!url.getProtocol().startsWith(
"file"
)) {
65
throw
new
RuntimeException(
"Cannot handle protocol from where this jar is loaded; "
+ root);
66
}
67
File f =
new
File(url.getFile());
68
file = f.getAbsolutePath().replace(
"\\"
,
"/"
);
69
// file = url.getPath();
70
int
i = file.lastIndexOf(
'!'
);
71
file = file.substring(0, i);
72
return
file;
73
}
else
if
(
"file"
.equals(root.getProtocol())) {
74
return
new
File(root.getPath()).getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getAbsolutePath();
75
}
else
{
76
throw
new
RuntimeException(
"Cannot find classpath for this class; class="
+ cls.getName() +
"; url="
+ root.toString());
77
}
78
}
79
}
org.ah.java.remotevmlauncher.ClasspathUtils
Definition
ClasspathUtils.java:32
org
ah
java
remotevmlauncher
ClasspathUtils.java
Generated by
1.18.0