質問編集履歴
3
ソースのコメント修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -204,11 +204,11 @@
|
|
204
204
|
ClassLoader classLoader = PropertyUtil.class.getClassLoader();
|
205
205
|
URL url = classLoader.getResource(fileName);
|
206
206
|
// 絶対パスの取得
|
207
|
+
// jarで実行した際のurlが「file:/xxx/yyy/...Test.jar!/property/test.properties」
|
208
|
+
// のようなurlになるためfile:やTest.jar!の!以降を取り除きたい(/xxx/yyy/...Test.jar のようにしたい)
|
207
209
|
String[] s = url.getPath().split(":");
|
208
210
|
String path = s[s.length - 1].split("!")[0];
|
209
|
-
|
211
|
+
|
210
|
-
//String path = url.getPath();
|
211
|
-
|
212
212
|
File file = new File(path);
|
213
213
|
Properties properties = new Properties();
|
214
214
|
try (JarFile jarFile = new JarFile(file);) {
|
2
実現したソースに修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -123,4 +123,120 @@
|
|
123
123
|
util/PropertyUtil.class
|
124
124
|
property/test.property
|
125
125
|
.project
|
126
|
+
```
|
127
|
+
★実現したソース★
|
128
|
+
```
|
129
|
+
修正対象
|
130
|
+
TestReadJarService.java
|
131
|
+
PropertyUtil.java
|
132
|
+
test.properties(test.propertyからファイル名変更)
|
133
|
+
property/test.properties(フォルダ構成変更)
|
134
|
+
```
|
135
|
+
TestReadJarService.java
|
136
|
+
```
|
137
|
+
package service;
|
138
|
+
|
139
|
+
import util.PropertyUtil;
|
140
|
+
|
141
|
+
public class TestReadJarService {
|
142
|
+
// パス修正
|
143
|
+
final String PROPERTY_FILE = "property/test.properties";
|
144
|
+
public TestReadJarService() {
|
145
|
+
Controller();
|
146
|
+
}
|
147
|
+
private void Controller() {
|
148
|
+
outputName();
|
149
|
+
outputYear();
|
150
|
+
// 追加処理
|
151
|
+
callJar();
|
152
|
+
}
|
153
|
+
private void outputName() {
|
154
|
+
String name = new PropertyUtil().getProperty(PROPERTY_FILE,"Name");
|
155
|
+
System.out.println(name);
|
156
|
+
}
|
157
|
+
private void outputYear() {
|
158
|
+
String year = new PropertyUtil().getProperty(PROPERTY_FILE,"Year");
|
159
|
+
System.out.println(year);
|
160
|
+
}
|
161
|
+
// 追加メソッド
|
162
|
+
private void callJar() {
|
163
|
+
String gender = new PropertyUtil().getPropertyJar(PROPERTY_FILE,"Gender");
|
164
|
+
System.out.println(gender);
|
165
|
+
}
|
166
|
+
}
|
167
|
+
```
|
168
|
+
PropertyUtil.java
|
169
|
+
```
|
170
|
+
package util;
|
171
|
+
|
172
|
+
import java.io.File;
|
173
|
+
import java.io.FileInputStream;
|
174
|
+
import java.io.IOException;
|
175
|
+
import java.io.InputStream;
|
176
|
+
import java.net.URL;
|
177
|
+
import java.util.Enumeration;
|
178
|
+
import java.util.Properties;
|
179
|
+
import java.util.jar.JarEntry;
|
180
|
+
import java.util.jar.JarFile;
|
181
|
+
|
182
|
+
public class PropertyUtil {
|
183
|
+
public String getProperty(String fileName, String key) {
|
184
|
+
Properties properties = new Properties();
|
185
|
+
String path = getPath(fileName);
|
186
|
+
try (InputStream property = new FileInputStream(path)) {
|
187
|
+
properties.load(property);
|
188
|
+
return properties.getProperty(key);
|
189
|
+
} catch (IOException e) {
|
190
|
+
e.printStackTrace();
|
191
|
+
return "failed";
|
192
|
+
}
|
193
|
+
}
|
194
|
+
|
195
|
+
// 修正あり
|
196
|
+
private String getPath(String fileName) {
|
197
|
+
ClassLoader classLoader = PropertyUtil.class.getClassLoader();
|
198
|
+
URL url = classLoader.getResource(fileName);
|
199
|
+
return url.getPath();
|
200
|
+
}
|
201
|
+
// 追加メソッド
|
202
|
+
public String getPropertyJar(String fileName,String key) {
|
203
|
+
|
204
|
+
ClassLoader classLoader = PropertyUtil.class.getClassLoader();
|
205
|
+
URL url = classLoader.getResource(fileName);
|
206
|
+
// 絶対パスの取得
|
207
|
+
String[] s = url.getPath().split(":");
|
208
|
+
String path = s[s.length - 1].split("!")[0];
|
209
|
+
/** なぜurl.getPath()ではだめなのか不明*/
|
210
|
+
//String path = url.getPath();
|
211
|
+
|
212
|
+
File file = new File(path);
|
213
|
+
Properties properties = new Properties();
|
214
|
+
try (JarFile jarFile = new JarFile(file);) {
|
215
|
+
Enumeration<JarEntry> entries = jarFile.entries();
|
216
|
+
while (entries.hasMoreElements()) {
|
217
|
+
JarEntry entry = entries.nextElement();
|
218
|
+
// jarに固められているクラスファイルやプロパティファイルを1件ずつ取得
|
219
|
+
String entryFileName = entry.getName();
|
220
|
+
//System.out.println("entryFileName="+entryFileName);
|
221
|
+
if (entryFileName != null && entryFileName.startsWith(fileName)) {
|
222
|
+
// 取得したファイル名と読み込む対象の名が一致した場合
|
223
|
+
try (InputStream property = classLoader.getResourceAsStream(entryFileName);) {
|
224
|
+
properties.load(property);
|
225
|
+
}
|
226
|
+
}
|
227
|
+
}
|
228
|
+
return properties.getProperty(key);
|
229
|
+
} catch (IOException ex) {
|
230
|
+
// 例外処理
|
231
|
+
return "Failed";
|
232
|
+
}
|
233
|
+
}
|
234
|
+
}
|
235
|
+
```
|
236
|
+
test.properties
|
237
|
+
```
|
238
|
+
Name=TAROU
|
239
|
+
Year=10
|
240
|
+
#追加
|
241
|
+
Gender=BOY
|
126
242
|
```
|
1
タイトル修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
jarファイル内
|
1
|
+
jarファイル内のプロパティファイルを参照する方法について
|
body
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
jarファイル内に存在するプロパティファイルの読み込みを行いたい
|
2
|
+
jarファイル内に存在するプロパティファイルの読み込みを行いたいです。
|
3
3
|
|
4
4
|
### 発生している問題・エラーメッセージ
|
5
5
|
ソースを実行するとプロパティファイルに記述されている内容の出力を行えるのですがjarファイルで実行するとFileNotFoundExceptionとファイルが見つからないと言われてしまいます。
|
6
|
+
下記のソースをどう修正したら良いでしょうか。
|
6
7
|
|
7
8
|
エラーメッセージ
|
8
9
|
```
|