I want to read properties files inside the jar file. I read many threads about this, but it don't solved the problem. I use the following method to do that:
public InputStream getLanguageFile(String languageName) {
languageName = languageName.replace(".properties", "");
return MainClass.class.getResourceAsStream("/me/s3ns3iw00/resources/languages/" + languageName + ".properties");
}
I get the following error:
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
Sorry for my bad english, and thanks for help!
getResourceAsStream
is expecting a path that is inside the ResourceFile
no need to provide your project folder path.
public InputStream getLanguageFile(String languageName) throws URISyntaxException {
languageName = languageName.replace(".properties", "");
return MainClass.class.getResourceAsStream("languages\" + languageName + ".properties");
}
So I found the solution.
I use this method to get the file's InputStream what is in your application's jar:
public InputStream getLanguageStream(String languageName) {
languageName = languageName.replace(".properties", "");
return MainClass.class.getResourceAsStream("/me/s3ns3iw00/resources/languages/" + languageName + ".properties");
}
Now you can load it to Properties:
properties.load(new InputStreamReader(getLanguageStream("ENG"), Charset.forName("UTF-8")));
And an extra:
I use this method to get files' name in jar path:
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;
}
You get a list of jarPath's directory content what is in your applications's jar.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.