2016-11-25 74 views
0

我刚刚在Windows上Netbeans的IDE中编写了一个电报机器人编程。运行完全没有问题。我的问题是,当我要上传我自己的Ubuntu的VPS中创建一个包的店,然后我运行它(使用Java的罐子PastaBot.jar),控制台显示此错误:MissingResourceException,但资源不会丢失

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> 

这是类LocalisationService我用它来得到正确的语言环境字符串,如可以在单构造我找到(正确的,我想)看到本地化文件:

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被正确定位,所以我不能得到正在发生的事情。我一直在这个问题上坚持到昨天,这让我很生气。

愿你们帮助我吗?谢谢:)

+0

“导致:java.lang.IllegalArgumentException:格式错误的\ uxxxx编码。”另请参阅http://stackoverflow.com/questions/16863301/exception-thrown-while-building-the-java-application-using-netbeans – 2016-11-25 18:35:32

+0

我从来没有在文件路径中使用任何像\这样的字符在我的代码中,唯一指定的是您可以在LocalisationService中看到的贴在下面,但实际上我使用了点,而不是/或\。我使用字符\只是在字符串作为unescape字符,但这不应该是一个问题... – A7X

+0

我发现可能是我的文件build.xml不包括strings.properties文件时,我建立jar。顺便说一句,我不知道我是否认为是正确的...我不知道如何解决它 – A7X

回答

1

我找到了解决办法编辑我strings.properties文件: 串像

hello\user 

抛出抛出:IllegalArgumentException这会导致程序崩溃,所以它不会加载任何操作(这就是为什么它可以检索strings_en.properties)。 取而代之的是,更好地逃避是“\”是这样的:

hello\\user 

这是因为该程序试图\用户编码为十六进制值,这是不。一个包含大量非拉丁字符的字符串的属性文件,例如一行ascii表情符号,可能是一个真正难以解决的问题,因为\ u字符是常用的。