我想读取jar文件中的属性文件。我读了很多有关此的主题,但是并没有解决问题。我使用以下方法来做到这一点:
public InputStream getLanguageFile(String languageName) {
languageName = languageName.replace(".properties", "");
return MainClass.class.getResourceAsStream("/me/s3ns3iw00/resources/languages/" + languageName + ".properties");
}
我收到以下错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 4: file:\C:\Users\danie\Desktop\mcserverlauncher_resources\setup\mcserverlauncher.jar!\me\s3ns3iw00\resources\languages
at sun.nio.fs.WindowsPathParser.normalize(Unknown Source)
at sun.nio.fs.WindowsPathParser.parse(Unknown Source)
at sun.nio.fs.WindowsPathParser.parse(Unknown Source)
at sun.nio.fs.WindowsPath.parse(Unknown Source)
at sun.nio.fs.WindowsFileSystem.getPath(Unknown Source)
at java.io.File.toPath(Unknown Source)
at me.s3ns3iw00.windows.Welcome.<init>(Welcome.java:59)
at me.s3ns3iw00.MainClass.start(MainClass.java:44)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application me.s3ns3iw00.MainClass
对不起,我的英语不好,谢谢你的帮助!
答案 0 :(得分:0)
getResourceAsStream
期望ResourceFile
内的路径,而无需提供您的项目文件夹路径。
public InputStream getLanguageFile(String languageName) throws URISyntaxException {
languageName = languageName.replace(".properties", "");
return MainClass.class.getResourceAsStream("languages\" + languageName + ".properties");
}
答案 1 :(得分:0)
所以我找到了解决方法。
我使用此方法来获取文件的InputStream,即您应用程序的jar中的内容:
public InputStream getLanguageStream(String languageName) {
languageName = languageName.replace(".properties", "");
return MainClass.class.getResourceAsStream("/me/s3ns3iw00/resources/languages/" + languageName + ".properties");
}
现在您可以将其加载到“属性”:
properties.load(new InputStreamReader(getLanguageStream("ENG"), Charset.forName("UTF-8")));
还有一个额外的内容:
我使用这种方法在jar路径中获取文件名:
public List<String> getFileNamesInJarPath(String jarPath) {
List<String> fileNames = new ArrayList<>();
CodeSource src = MainClass.class.getProtectionDomain().getCodeSource();
try {
if (src != null) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
while (true) {
ZipEntry e = zip.getNextEntry();
if (e == null)
break;
String name = e.getName();
if (name.startsWith(jarPath)) {
String substringedName = name.substring(name.lastIndexOf("/") + 1);
if (!substringedName.equalsIgnoreCase("")) {
fileNames.add(substringedName);
}
}
}
} else {
/* Fail... */
}
} catch (IOException e) {
e.printStackTrace();
}
return fileNames;
}
您将获得jarPath目录内容的列表,这些目录内容位于应用程序的jar中。