私はアプリケーション(特にBukkit Minecraftサーバー用のプラグイン)を作成しています。これを行うには、アプリケーションのJARから.propertiesファイルにアクセスする必要があります。これは私が奇妙な問題に直面するところです。開発用PCでプログラムをテストすると、うまく動作します。 .propertiesファイルがロードされ、すべて正常です。しかし、私がテストしている他のコンピュータでは、アプリケーションを起動しようとしましたが、プロパティをロードできず、InputStream
はnull
です。ファイルをロードする方法は次のとおりです。すべてのコンピュータ上のJAR内のリソースにアクセスできない
public class Points {
private HashMap<String, MessageFormat> messages;
public Points() {
buildMessages();
}
public static void buildMessages() {
Properties messageProps = new Properties();
InputStream in = Points.class.getResourceAsStream("resources/messages.properties");
messages = new HashMap<String, MessageFormat>();
Enumeration en;
try {
messageProps.load(in);
} catch(IOException ex) {
System.err.println("Couldn't read message properties file!");
return;
} catch(NullPointerException ex) {
System.err.println("Couldn't read message properties file!");
if(in == null)
System.out.println("IOStream null");
return;
}
en = messageProps.propertyNames();
while(en.hasMoreElements()) {
String key = (String)en.nextElement();
String prop = messageProps.getProperty(key);
MessageFormat form = new MessageFormat(prop.replaceAll("&",
"\u00a7").replaceAll("`", ""));
messages.put(key, form);
}
}
}
私はいくつかの無関係のコードを省略しましたが、それはその要点です。次のようにJARの構造は次のとおりです。私のPC上で
com/
pvminecraft/
points/
Points.java <-- The class where the file is loaded
resources/
messages.properties <-- The file being loaded
ファイルがresources/messages.properties
からロードされたが、他のファイルに、InputStream
はnullで、かつNullPointerException
のための私のcatch
ブロックが実行されています。何が問題を引き起こしているのでしょうか、どのように修正できますか?ありがとう。
更新:フルパス(/com/pvminecraft/points/resources/messages.properties
)を使用しても、同じ問題は引き続き発生します。
アップデート2:ここではフルスタックトレースです:
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:435)
at java.util.Properties.load0(Properties.java:354)
at java.util.Properties.load(Properties.java:342)
at com.pvminecraft.points.Points.buildMessages(Unknown Source)
at com.pvminecraft.points.Points.onEnable(Unknown Source)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:188)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:968)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:280)
at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:186)
at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:169)
at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:436)
at org.bukkit.Bukkit.reload(Bukkit.java:187)
at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:22)
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:165)
at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:378)
at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:374)
at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:564)
at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:541)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
org.bukkit
とorg.craftbukkit
もののすべてがサーバです。 .propertiesファイルはonEnable
メソッドで呼び出されたbuildMessages
メソッドにロードされ、Points
です。
更新3:アーチLinuxの新規インストール時に、メッセージプロパティファイルが正しくロードされ、すべて正常です。リモートサーバーはUbuntu Linuxで、私の開発PCはArchです。
更新4:申し訳ありませんが、これは解決の一種です。それはローカライズされた問題のようです。私はそれが2台のコンピュータにアクセスすることができたので、そのプログラムは両方で正しく動作するからです。それは迷惑なことですが、これは私のコードやビルドスクリプトに間違っているようには見えません。私はまだ何が間違っているかを知りたがっていますが、これ以上のことはありません。私はこれを調べ続けます。みんな、ありがとう。
ディレクトリ "resources"はクラスパス上にありません - Tomの答えを参照してください。しかし、 "com/pvminecraft/points/resources"というディレクトリがあります。あなたの開発環境をチェックしてください - クラスパスが面白く設定されているか、クラスパス上にあるプロパティファイルのコピーがあると思います。 – Paul
スタックトレース全体を送信してください。 – Paul
行番号はここにあります: 'at com.pvminecraft.points.Points.buildMessages(Unknown Source)'。あなたのJDKのバージョンは何ですか? – Kowser