回答編集履歴
3
別の方法を追記した。
answer
CHANGED
@@ -12,4 +12,16 @@
|
|
12
12
|
|
13
13
|
File canonicalFile = file.getCanonicalFile();
|
14
14
|
System.out.println(canonicalFile.getParent());
|
15
|
+
```
|
16
|
+
|
17
|
+
|
18
|
+
### 別の方法
|
19
|
+
以下の方法でも、jarの場所を取得できるはずです。
|
20
|
+
file:/tmp/sample.jar!/example/MyClass.class
|
21
|
+
のような形で取得できるので、:から!までを切り出せばよいです。
|
22
|
+
|
23
|
+
```lang-java
|
24
|
+
URL resourceJarUrl = MyClass.class.getResource("MyClass.class");
|
25
|
+
String resourceJarPath = resourceJarUrl.getFile();
|
26
|
+
resourceJarPath = URLDecoder.decode(resourceJarPath, "UTF-8");
|
15
27
|
```
|
2
複数行で実行するように修正した。
answer
CHANGED
@@ -4,6 +4,12 @@
|
|
4
4
|
にある、以下の方法で取得できました。
|
5
5
|
|
6
6
|
```lang-java
|
7
|
-
|
7
|
+
ProtectionDomain protectionDomain = MyClass.class.getProtectionDomain();
|
8
|
+
CodeSource codeSource = protectionDomain.getCodeSource();
|
9
|
+
URL location = codeSource.getLocation();
|
10
|
+
URI uri = location.toURI();
|
11
|
+
File file = new File(uri.getPath());
|
12
|
+
|
13
|
+
File canonicalFile = file.getCanonicalFile();
|
8
|
-
|
14
|
+
System.out.println(canonicalFile.getParent());
|
9
15
|
```
|
1
ディレクトリを取得するコードに修正した。
answer
CHANGED
@@ -5,5 +5,5 @@
|
|
5
5
|
|
6
6
|
```lang-java
|
7
7
|
File file = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
|
8
|
-
System.out.println(file.
|
8
|
+
System.out.println(file.getCanonicalFile().getParent());
|
9
9
|
```
|