gpt4 book ai didi

java - MissingResourceException 但资源并未丢失

转载 作者:行者123 更新时间:2023-12-01 09:12:50 24 4
gpt4 key购买 nike

我刚刚在 Windows Netbeans 上的 IDE 中完成了 Telegram 机器人的编程。运行完美,没有任何问题。我的问题是,当我创建一个存储包以便将其上传到我自己的 Ubuntu VPS 中然后运行它(使用 java -jar PastaBot.jar)时,控制台显示此错误:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name localisation.strings, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:1082)
at utils.LocalisationService.<init>(LocalisationService.java:74)
at utils.LocalisationService.getInstance(LocalisationService.java:62)
at pastabot.Pasta.populateDongers(Pasta.java:53)
at pastabot.Pasta.<init>(Pasta.java:29)
at pastabot.BotHandler.<init>(BotHandler.java:20)
at pastabot.TelegramBotJava.main(TelegramBotJava.java:26)
Caused by: java.lang.IllegalArgumentException: Malformed \uxxxx encoding.
at java.util.Properties.loadConvert(Properties.java:574)
at java.util.Properties.load0(Properties.java:391)
at java.util.Properties.load(Properties.java:341)
at java.util.PropertyResourceBundle.<init>(PropertyResourceBundle.java:138)
at java.util.ResourceBundle$Control.newBundle(ResourceBundle.java:2687)
at java.util.ResourceBundle.loadBundle(ResourceBundle.java:1501)
at java.util.ResourceBundle.findBundle(ResourceBundle.java:1465)
at java.util.ResourceBundle.findBundle(ResourceBundle.java:1419)
at java.util.ResourceBundle.findBundle(ResourceBundle.java:1419)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1361)
... 7 more

我不明白为什么我的 IDE 中一切正常,但在我的服务器中却无法启动。我知道这是与我的属性文件相关的问题,并且在大多数情况下,由于 strings.properties 文件位于错误的路径中而引发。

这是我的文件层次结构:

file folder structure

这是我的 build.xml 文件:也许我缺少其中的一个属性,但我不知道如何解决这个问题,因为我不太喜欢 xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<project name="Copypastabot" default="default" basedir=".">
<description>Builds, tests, and runs the project Copypastabot.</description>
<import file="nbproject/build-impl.xml"/>

<target name="package-for-store" depends="jar">
<property name="store.jar.name" value="PastaBot"/>
<property name="store.dir" value="store"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
<delete dir="${store.dir}"/>
<mkdir dir="${store.dir}"/>
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
<zip destfile="${store.jar}">
<zipfileset src="${store.dir}/temp_final.jar"
excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${store.dir}/temp_final.jar"/>
</target>

这是我用来获取正确的语言环境字符串的 LocalizationService 类,正如您在单例构造函数中看到的那样,我找到了(我认为正确的)本地化文件:

package utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;


public class LocalisationService {
private static LocalisationService instance = null;
private final HashMap<String, String> supportedLanguages = new HashMap<>();

private ResourceBundle english;
private ResourceBundle italian;

private class CustomClassLoader extends ClassLoader {
public CustomClassLoader(ClassLoader parent) {
super(parent);

}

public InputStream getResourceAsStream(String name) {
InputStream utf8in = getParent().getResourceAsStream(name);
if (utf8in != null) {
try {
byte[] utf8Bytes = new byte[utf8in.available()];
utf8in.read(utf8Bytes, 0, utf8Bytes.length);
byte[] iso8859Bytes = new String(utf8Bytes, "UTF-8").getBytes("ISO-8859-1");
return new ByteArrayInputStream(iso8859Bytes);
} catch (IOException e) {
e.printStackTrace();

} finally {
try {
utf8in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}


public static LocalisationService getInstance() {
if (instance == null) {
synchronized (LocalisationService.class) {
if (instance == null) {
instance = new LocalisationService();
}
}
}
return instance;
}


private LocalisationService() {
CustomClassLoader loader = new CustomClassLoader(Thread.currentThread().getContextClassLoader());
english = ResourceBundle.getBundle("localisation.strings", new Locale("en", "US"), loader);
supportedLanguages.put("en", "English");
italian = ResourceBundle.getBundle("localisation.strings", new Locale("it", "IT"), loader);
supportedLanguages.put("it", "Italiano");
}

/**
* Get a string in default language (en)
* @param key key of the resource to fetch
* @return fetched string or error message otherwise
*/
public String getString(String key) {
String result;
try {
result = english.getString(key);
} catch (MissingResourceException e) {
result = "String not found";
}

return result;
}

/**
* Get a string in default language
* @param key key of the resource to fetch from localisations
* @param language code key for language (such as "EN" for english)
* @return fetched string or error message otherwise
*/
public String getString(String key, String language) {
String result;
try {
switch (language.toLowerCase()) {
case "en":
result = english.getString(key);
break;
case "it":
result = italian.getString(key);
break;
default:
result = english.getString(key);
break;
}
} catch (MissingResourceException e) {
result = english.getString(key);
}

return result;
}

public HashMap<String, String> getSupportedLanguages() {
return supportedLanguages;
}

public String getLanguageCodeByName(String language) {
return supportedLanguages.entrySet().stream().filter(x -> x.getValue().equals(language)).findFirst().get().getKey();
}
}

我已经寻找解决方案,但没有任何帮助:strings_en.properties 位置正确,所以我无法了解发生了什么。直到昨天我才被这个问题困扰,这让我发疯。

你们可以帮帮我吗?谢谢:)

最佳答案

我找到了编辑 strings.properties 文件的解决方案:像这样的字符串

hello\user

抛出 IllegalArgumentException ,这会导致程序崩溃,因此它不会加载任何内容(这就是它无法检索 strings_en.properties 的原因)。相反,最好像这样转义“\”:

hello\\user

这是因为程序尝试将\user 编码为十六进制值,但事实并非如此。包含大量由非拉丁字符组成的字符串(例如一行 ascii 笑脸)的属性文件可能是一个真正麻烦的问题,因为通常使用\u 字符。

关于java - MissingResourceException 但资源并未丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40810668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com