Trying to code a Minecraft Plugin and when I run the /fakeop in game I get an external error. The /fakeop (playername) works though
public class CortexTroll extends JavaPlugin {
#Override
public void onEnable() {}
#Override
public void onDisable() {}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("fakeop")) {
Player player = (Player) sender;
Player target = Bukkit.getServer().getPlayer(args[0]);
if (args.length == 0) {
player.sendMessage("Specify a player to Op. /fakeop <target>");
return true;
}
player.sendMessage("Fake Opped " + args[0]);
Bukkit.broadcastMessage(ChatColor.GRAY + "[" + player.getName() + ": Opped " + args[0] + "]");
target.sendMessage(ChatColor.YELLOW + "You are now op!");
}
return true;
}
}
Error:
Illuminatiiiiii issued server command: /fakeop [21:33:10 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing
command 'fakeop' in plugin CortexTroll v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
~[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
~[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646)
~[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1351)
[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1186)
[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)
[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)
[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13)
[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733)
[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399)
[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672)
[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571)
[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_91] Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at us.thecortex.cortextroll.CortexTroll.onCommand(CortexTroll.java:29)
~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
~[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
Because im not allowed to comment jet, here is what you could try: Move the line below after the if-statement that checks if args.length is 0
public Class CortexTroll extends JavaPlugin{
#Override
public void onEnable() {}
#Override
public void onDisable() {}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("fakeop")) {
Player player = (Player) sender;
if (args.length == 0) {
player.sendMessage("Specify a player to Op. /fakeop <target>");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]); // Is now below the if-statement
player.sendMessage("Fake Opped " + args[0]);
Bukkit.broadcastMessage(ChatColor.GRAY + "[" + player.getName() + ": Opped " + args[0] + "]");
target.sendMessage(ChatColor.YELLOW + "You are now op!");
}
return true;
}
}
Why should this fix the error?
If you execute /fakeop without any argument, args has the length of 0. If you try to access the args[0], you get a ArrayIndexOutOfBoundsException because it wants a Array with length 1.
EDIT: Thanks for pointing out that it isn't a NullPointer.
Related
I am writing a Iiop server and I encountered the following error when I try to run it:
Exception in thread "main" javax.naming.ConfigurationException: Problem with PortableRemoteObject.toStub(); object not exported or stub not found [Root exception is java.rmi.NoSuchObjectException: object not exported]
at com.sun.jndi.toolkit.corba.CorbaUtils.remoteToCorba(CorbaUtils.java:105)
at com.sun.jndi.cosnaming.RemoteToCorba.getStateToBind(RemoteToCorba.java:79)
at javax.naming.spi.NamingManager.getStateToBind(NamingManager.java:870)
at com.sun.jndi.cosnaming.CNCtx.callBindOrRebind(CNCtx.java:600)
at com.sun.jndi.cosnaming.CNCtx.rebind(CNCtx.java:713)
at com.sun.jndi.cosnaming.CNCtx.rebind(CNCtx.java:730)
at javax.naming.InitialContext.rebind(InitialContext.java:433)
at ExecRmiIiopServ.<init>(ExecRmiIiopServ.java:13)
at ExecRmiIiopServ.main(ExecRmiIiopServ.java:20)
Caused by: java.rmi.NoSuchObjectException: object not exported
at sun.rmi.transport.ObjectTable.getStub(ObjectTable.java:124)
at java.rmi.server.RemoteObject.toStub(RemoteObject.java:106)
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.toStub(PortableRemoteObject.java:156)
at javax.rmi.PortableRemoteObject.toStub(PortableRemoteObject.java:116)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jndi.toolkit.corba.CorbaUtils.remoteToCorba(CorbaUtils.java:99)
... 8 more
From what I've read about this error, I need a strongly typed declarations of an object so the Garbage Collector won't discard it, hence why I tried to add a functions object that does just that, but still the error persists, both in the normal run and with a breakpoint, the error triggering at the ctx.rebind line.
public class ExecRmiIiopServ {
public ExecRmiIiopServ(String host, String port, String nume) throws Exception {
Runtime.getRuntime().exec("cmd /c start orbd -ORBInitialPort 1050");
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
props.setProperty("java.naming.provider.url", "iiop://" + host + ":" + port);
ExecRmiInte functions = new ExecRmiImpl();
Context ctx = new InitialContext(props);
ctx.rebind(nume, functions);
System.out.println("Java RMI IIOP waiting: " + host + ":" + port + "/" + nume);
}
public static void main(String args[]) throws Exception {
if (args.length > 2)
new ExecRmiIiopServ(args[0], args[1], args[2]);
else
new ExecRmiIiopServ("localhost", "1050", "ExecIiop");
}
}
The ExecRmiImpl() class is just this for now:
public class ExecRmiImpl implements ExecRmiInte {
public ExecRmiImpl() { }
}
What am I doing wrong?
I am trying to implement a simple code to read the data from the ultrasonic and send it to a server by using Californium. The problem is that when I use debug, it doesn't have any error. However when I export the code to a runable jar file and run it on my raspberry pi, it throws the following errors:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: org/eclipse/californium/elements/util/NamedThreadFactory
at org.eclipse.californium.core.CoapServer.<init>(CoapServer.java:163)
at org.eclipse.californium.core.CoapServer.<init>(CoapServer.java:120)
at iot.main.device.DeviceClient.main(DeviceClient.java:34)
... 5 more
Caused by: java.lang.ClassNotFoundException: org.eclipse.californium.elements.util.NamedThreadFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 8 more
I am not sure what caused this and how to solve it, any suggestion would be much helpful. Below is the code:
package iot.main.device;
import static org.eclipse.californium.core.coap.CoAP.ResponseCode.BAD_REQUEST;
import static org.eclipse.californium.core.coap.CoAP.ResponseCode.CHANGED;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.server.resources.CoapExchange;
import com.pi4j.io.gpio.*;
public class DeviceClient {
private static GpioPinDigitalOutput sensorTriggerPin ;
private static GpioPinDigitalInput sensorEchoPin ;
final static GpioController gpio = GpioFactory.getInstance();
public static void main(String[] args) {
double ultraVal;
sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04); // Trigger pin as OUTPUT
sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05,PinPullResistance.PULL_DOWN); // Echo pin as INPUT
CoapClient client = new CoapClient("coap://localhost/Ultrasonic");
CoapServer server = new CoapServer();
server.add(new UltraResource());
server.start();
while(true){
try {
Thread.sleep(2000);
sensorTriggerPin.high(); // Make trigger pin HIGH
Thread.sleep((long) 0.00001);// Delay for 10 microseconds
sensorTriggerPin.low(); //Make trigger pin LOW
while(sensorEchoPin.isLow()){ //Wait until the ECHO pin gets HIGH
}
long startTime= System.nanoTime(); // Store the surrent time to calculate ECHO pin HIGH time.
while(sensorEchoPin.isHigh()){ //Wait until the ECHO pin gets LOW
}
long endTime= System.nanoTime(); // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
ultraVal = ((((endTime - startTime)/1e3)/2) / 29.1);
System.out.println("Distance : " + ultraVal + " cm"); //Printing out the distance in cm
Thread.sleep(1000);
CoapResponse response = client.put(Double.toString(ultraVal), MediaTypeRegistry.TEXT_PLAIN);
if (response!=null) {
System.out.println( response.getCode() );
System.out.println( response.getOptions() );
System.out.println( response.getResponseText() );
} else {
System.out.println("Request failed");
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static class UltraResource extends CoapResource {
public String value = "Resource has not changed yet !!!";
public UltraResource() {
super("Ultrasonic");
setObservable(true);
}
#Override
public void handleGET(CoapExchange exchange) {
exchange.respond(value);
}
#Override
public void handlePUT(CoapExchange exchange) {
String payload = exchange.getRequestText();
System.out.println(payload + " cm");
try {
exchange.respond(CHANGED, payload);
value = new String(payload);
changed();
} catch (Exception e) {
e.printStackTrace();
exchange.respond(BAD_REQUEST, "Invalid String");
}
}
}
}
The error states that
Caused by: java.lang.ClassNotFoundException: org.eclipse.californium.elements.util.NamedThreadFactory
This means you are writing code that uses a dependency from Californium. That dependency is within your dev. environment but it is not within the runtime environment of your Raspberry Pi.
Have a look at this other post which may help you. Also this SO site may be better.
My Bukkit plugin command always throws an exception when I run it.
When I type my command: /config set (it should save info to file/config), I get this error in chat:
An internal error occurred while attemping to perform this command.
Why does this happen?
Console Log:
[13:23:24 INFO]: whispereq issued server command: /config set
[13:23:24 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'config' in plugin registyPlayer v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) ~[src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) ~[src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:952) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at whispereq.saver.onCommand(saver.java:14) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
... 13 more
My code from the saver class:
package whispereq;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class saver implements CommandExecutor {
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("config")) {
if (sender instanceof Player) {
if (args.length == 1) {
if (args[1].equalsIgnoreCase("set")) {
sender.sendMessage("§6Registering player§c " + args[0] + "§6 to Server....");
Player p = (Player) sender;
String nick = p.getName().toLowerCase();
String uid = p.getUniqueId().toString();
boolean op = p.isOp();
GameMode gm = p.getGameMode();
GameMode dgm = Bukkit.getDefaultGameMode();
float exp = p.getExp();
float explvl = p.getExpToLevel();
Main.getInst().getConfig().set("players." + nick + ".uuid", uid);
Main.getInst().getConfig().set("players." + nick + ".isOpped", op);
Main.getInst().getConfig().set("players." + nick + ".CurrentGameMode", gm);
Main.getInst().getConfig().set("players." + nick + ".DefaultGameMode", dgm);
Main.getInst().getConfig().set("players." + nick + ".Exp", exp);
Main.getInst().getConfig().set("players." + nick + ".ExpLevel", explvl);
Main.getInst().saveConfig();
sender.sendMessage("§6Finished!, yours Current in-game Status was SUccesfully registered to the config.yml File in Plugin's Directory. use §c/registy getMe§6 to view your Property.");
return true;
} else if (args[1].equalsIgnoreCase("get")) {
Player p = (Player) sender;
String nick = p.getName().toLowerCase();
if (Main.getInst().getConfig().get("players." + nick) != null) {
p.sendMessage("§8_____________________________________________________");
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".uuid"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".isOpped"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".CurrentGameMode"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".DefaultGameMode"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".Exp"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + "ExpLevel"));
p.sendMessage("§8_____________________________________________________");
}
}
}
}
}
return false;
}
}
Main class:
package whispereq;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public static Main instance;
public void onEnable() {
instance = this;
System.out.println("Loading RegistyPlayer ..");
getCommand("config").setExecutor(new saver());
saveDefaultConfig();
}
public static Main getInst() {
return instance;
}
}
Config File (empty):
#-----------------------------------------------------------#
plugin.yml:
name: registyPlayer
version: 1.0
main: whispereq.Main
commands:
config:
Craftbukkit: 1.7.2 R03
Bukkit API: 1.7.2 R03
Server Craftbukkit: 1.7.2 R03
Arrays always start at index 0. Therefore, to access whether the player specified "get" or "set" you should replace
args[1].equalsIgnoreCase("set")
args[1].equalsIgnoreCase("get")
with
args[0].equalsIgnoreCase("set")
args[0].equalsIgnoreCase("get")
Also, not sure what you are doing with the line
sender.sendMessage("§6Registering player§c " + args[0] + "§6 to Server....");
args[0] refers to either "get" or "set". Presumably, you should replace it with "nick" and insert the line after
String nick = p.getName().toLowerCase();
Just wanted to try Cassandra Java driver from eclipse and copied a sample code from "Practical Cassandra"
But faced error below output by eclipse:
Exception in thread "main" java.lang.NoClassDefFoundError: io/netty/util/Timer
at com.datastax.driver.core.Configuration$Builder.build(Configuration.java:294)
at com.datastax.driver.core.Cluster$Builder.getConfiguration(Cluster.java:1247)
at com.datastax.driver.core.Cluster.<init>(Cluster.java:116)
at com.datastax.driver.core.Cluster.buildFrom(Cluster.java:181)
at com.datastax.driver.core.Cluster$Builder.build(Cluster.java:1264)
at SampleApp.connect(SampleApp.java:13)
at SampleApp.main(SampleApp.java:64)
Caused by: java.lang.ClassNotFoundException: io.netty.util.Timer
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
here is the sample code:
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
public class SampleApp {
private Cluster cluster;
private Session session;
public void connect(String node) {
cluster = Cluster.builder().addContactPoint(node).build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Cluster: %s\n", metadata.getClusterName());
for ( Host host : metadata.getAllHosts() ) {
System.out.printf("Host: %s \n",host.getAddress());
}
session = cluster.connect();
}
public void close(){
cluster.close();
}
public void createSchema(){
session.execute("CREATE KEYSPACE IF NOT EXISTS portfolio_demo " +
"WITH REPLICATION 5 { ‘class’: ‘SimpleStrategy’, " +
"'replication_factor’: 1 };");
session.execute("CREATE TABLE IF NOT EXISTS portfolio_demo.portfolio (" +
"portfolio_id UUID, ticker TEXT, " +
"current_price DECIMAL, current_change DECIMAL, " +
"current_change_percent FLOAT, " +
"PRIMARY KEY(portfolio_id, ticker));");
}
public void loadData(){
session.execute("INSERT INTO portfolio_demo.portfolio " +
"(portfolio_id, ticker, current_price, " +
" current_change, current_change_percent) VALUES " +
"(756716f7-2e54-4715-9f00-91dcbea6cf50, ‘GOOG’, " +
" 889.07, -4.00, -0.45);");
session.execute("INSERT INTO portfolio_demo.portfolio " +
"(portfolio_id, ticker, current_price, " +
" current_change, current_change_percent) VALUES " +
"(756716f7-2e54-4715-9f00-91dcbea6cf50, ‘AMZN’, " +
" 297.92, -0.94, -0.31);");
}
public void printResults(){
ResultSet results = session.execute("SELECT * FROM " +
"portfolio_demo.portfolio WHERE portfolio_id 5 " +
"756716f7-2e54-4715-9f00-91dcbea6cf50;");
for (Row row : results) {
System.out.println(String.format("%-7s\t%-7s\t%-7s\t%-7s \n%s",
"Ticker", "Price", "Change", "PCT",
"........1........1........1........"));
System.out.println(String.format("%-7s\t%0.2f\t%0.2f\t%0.2f",
row.getString("ticker"),
row.getDecimal("current_price"),
row.getDecimal("current_change"),
row.getFloat("current_change_percent") ));
}
}
public static void main(String[] args) {
SampleApp client = new SampleApp();
client.connect("127.0.0.1");
client.createSchema();
client.loadData();
client.printResults();
client.close();
}
}
And I also added several external JARs which are downloaded or comes with eclipse:
cassandra-driver-core-3.0.0.jar
guava-18.0.jar
netty-3.10.6.Final-20160303.120156-121.jar
org.apache.log4j_1.2.15.v201012070815.jar (from eclipse plugin)
org.slf4j.api_1.7.2.v20121108-1250.jar (from eclipse plugin)
org.slf4j.impl.log4j12_1.7.2.v20131105-2200.jar (from eclipse plugin)
I saw the same questions about the netty error but still could not figure out what was wrong with my code.
Thanks a lot.
This is the wrong Netty version. Version 3.0.0 of the driver uses 4.0.33.
You can view the driver's dependencies in the POM. The properties such as ${netty.version} are defined in the parent POM.
After I could eventually figure out why JWS 1.6.0_29 failed to launch a 1.4.2_12 application (see this question), I faced another exception when launching a 1.4.2_12 app. with JWS 1.6.0_29.
I get a MissingResourceException when loading a ResourceBundle. Yet a message.properties file do exists in the same package as the class that's loading it.
When JWS 1.4 or 1.5 is used to launch the application, the exception is not raised.
The exception is raised only when launching the app. with JWS 1.6.
Full stackstrace is :
java.lang.ExceptionInInitializerError
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.javaws.Launcher.executeApplication(Unknown Source)
at com.sun.javaws.Launcher.executeMainClass(Unknown Source)
at com.sun.javaws.Launcher.doLaunchApp(Unknown Source)
at com.sun.javaws.Launcher.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.util.MissingResourceException: Can't find bundle for base name com.test.hello.messages, locale fr_FR
at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
at java.util.ResourceBundle.getBundleImpl(Unknown Source)
at java.util.ResourceBundle.getBundle(Unknown Source)
at com.test.hello.Main.<clinit>(Main.java:10)
... 9 more
Test case to reproduce
JNLP descriptor is:
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://localhost:80/meslegacy/apps" href="testJwsXXTo142.jnlp">
<information>
<title>JWS TEST 1.6 -> 1.4.2</title>
<vendor>Hello World Vendor</vendor>
<description>Hello World</description>
</information>
<security>
<all-permissions />
</security>
<resources>
<j2se version="1.4.2_12" href="http://java.sun.com/products/autodl/j2se" />
<jar href="jar/helloworld.jar" main="true" />
</resources>
<application-desc main-class="com.test.hello.Main" />
</jnlp>
com.test.hello.Main class is:
package com.test.hello;
import java.util.ResourceBundle;
import javax.swing.JFrame;
public class Main {
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(Main.class.getPackage().getName()+".messages");
public static void main(String[] args) {
JFrame frame = new JFrame("Hello world !");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
}
}
Complementary tests
Specifying ClassLoader and Locale to the ResourceBundle.getBundle()
method does not fix the problem.
Main.class.getClassLaoder() and
Thread.currentThread().getContextClassLaoder() have been tested and spawn the same exception.
Loading resource "by hand" does work (see below).
Test code to load resource manually :
ClassLoader cl = Main.class.getClassLoader();
String resourcePath = baseName.replaceAll("\\.", "/");
System.out.println(resourcePath);
URL resourceUrl = cl.getResource(resourcePath+".properties");
System.out.println("Resource manually loaded :"+resourceUrl);
Will produce :
com/test/hello/messages.properties
Resource manually loaded :jar:http://localhost:80/meslegacy/apps/jar/helloworld.jar!/com%2ftest%2fhello%2fmessages.properties
However, while it is possible to find the resource, get the resource content is not.
Example:
ClassLoader cl = Main.class.getClassLoader();
String resourcePath = baseName.replaceAll("\\.", "/") + ".properties";
URL resourceUrl = cl.getResource(resourcePath);
// here, resourceUrl is not null. Then build bundle by hand
ResourceBundle prb = new PropertyResourceBundle(resourceUrl.openStream());
Which spawns :
java.io.FileNotFoundException: JAR entry com%2ftest%2fhello%2fmessages.properties not found in C:\Documents and Settings\firstname.lastname\Application Data\Sun\Java\Deployment\cache\6.0\18\3bfe5d92-3dfda9ef
at com.sun.jnlp.JNLPCachedJarURLConnection.connect(Unknown Source)
at com.sun.jnlp.JNLPCachedJarURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at com.test.hello.Main.main(Main.java:77)
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.javaws.Launcher.executeApplication(Unknown Source)
at com.sun.javaws.Launcher.executeMainClass(Unknown Source)
at com.sun.javaws.Launcher.doLaunchApp(Unknown Source)
at com.sun.javaws.Launcher.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Seems to be more a kind of cache issue...
I any of you had a hint, it would be greatly appreciated,
Thanks for reading.
Here is the explanation and workarround for this problem.
1 - Explanation
The problem comes from the URLs returned by the system ClassCloader (JWS6 system ClassLoader).
With JWS 1.6, URL returned by the system ClassLoader contain escape sequences such as the one shown in the following :
jar:http://localhost:80/meslegacy/apps/jar/helloworld.jar!/com%2ftest%2fhello%2fmessages.properties
Locating resources in classpath is possible but when it comes to actually access the content of that resource a FileNotFoundException is raised: This is what causes the FileNotFoundException in ResourceBundle.
Please note that when no escape sequence appears in the URL, for example when the resource is at the root of the claspath, there is no problem to access the resource content. Problem appears only when you get %xx stuff in the URL path part.
2 - Workarround
Once the problem had been focused (it took me days to figure this out !), it was time to find a workarround for this.
While it would have been possible for me to fix my problem on specific localized code parts, it quickly turned out that is was possible to fix the issue globaly by coding a specific ClassLoader to "replace" the JNLPClassLoader.
I don't acutally "replace" because it seems impossible to me but I rather do the following :
Disable SecurityManager to be abled to play with my custom
classloader
Code my own classloader derived from URLClassLoader that fix URL when they are returned
Set its classpath with the claspath extracted from the
JNLPClassLoader
Set this custom classloader to be the context classloader
Set this custom classloader to be the AWT-Event-Thread context
classloader
Use this custom classloader to load my application entry point.
This gives the following ClassLoader
public class JwsUrlFixerClassLoader extends URLClassLoader {
private final static Logger LOG = Logger.getLogger(JwsUrlFixerClassLoader.class);
private static String SIMPLE_CLASS_NAME = null;
private static boolean LOG_ENABLED = "true".equals(System.getProperty("classloader.debug"));
static {
SIMPLE_CLASS_NAME = JwsUrlFixerClassLoader.class.getName();
int idx = SIMPLE_CLASS_NAME.lastIndexOf('.');
if (idx >= 0 && idx < SIMPLE_CLASS_NAME.length()-1) {
SIMPLE_CLASS_NAME = SIMPLE_CLASS_NAME.substring(idx + 1);
}
}
public JwsUrlFixerClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
public URL getResource(String name) {
if (LOG.isDebugEnabled()) {
LOG.debug("getResource(): getResource(" + name + ")");
}
if (LOG_ENABLED) {
login("getResource(" + name + ")");
}
URL out = super.getResource(name);
if (out != null) {
out = URLFixerTool.fixUrl(out);
}
if (LOG_ENABLED) {
logout("getResource returning " + out);
}
return out;
}
public URL findResource(String name) {
if (LOG_ENABLED) {
login("findResource(" + name + ")");
}
URL out = super.findResource(name);
if (out != null) {
out = URLFixerTool.fixUrl(out);
}
if (LOG_ENABLED) {
logout("findResource returning " + out);
}
return out;
}
public InputStream getResourceAsStream(String name) {
if (LOG_ENABLED) {
login("getResourceAsStream(" + name + ")");
}
InputStream out = super.getResourceAsStream(name);
if (LOG_ENABLED) {
logout("getResourceAsStream returning " + out);
}
return out;
}
protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (LOG_ENABLED) {
login("loadClass(" + name + ")");
}
// First, check if the class has already been loaded
Class c = findLoadedClass(name);
if (c == null) {
try {
c = findClass(name);
} catch (ClassNotFoundException cnfe) {
if (getParent() == null) {
// c = findBootstrapClass0(name);
Method m = null;
try {
m = URLClassLoader.class.getMethod("findBootstrapClass0", new Class[] {});
m.setAccessible(true);
c = (Class) m.invoke(this, new Object[] { name });
} catch (Exception e) {
throw new ClassNotFoundException();
}
} else {
c = getParent().loadClass(name);
}
}
}
if (resolve) {
resolveClass(c);
}
if (LOG_ENABLED) {
logout("loadClass returning " + c);
}
return c;
}
private static void login(String message) {
System.out.println("---> [" + Thread.currentThread().getName() + "] " + SIMPLE_CLASS_NAME + ": " + message);
}
private static void logout(String message) {
System.out.println("<--- [" + Thread.currentThread().getName() + "] " + SIMPLE_CLASS_NAME + ": " + message);
}
}
Now in a AppBoostrap class which I set to be the main-class in the JNLP descriptor, I do the following :
System.setSecurityManager(null);
ClassLoader parentCL = AppBootstrap.class.getClassLoader();
URL[] classpath = new URL[] {};
if (parentCL instanceof URLClassLoader) {
URLClassLoader ucl = (URLClassLoader) parentCL;
classpath = ucl.getURLs();
}
final JwsUrlFixerClassLoader vlrCL = new JwsUrlFixerClassLoader(classpath, parentCL);
Thread.currentThread().setContextClassLoader(vlrCL);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
Thread.currentThread().setContextClassLoader(vlrCL);
}
});
} catch (Exception e) {
LOG.error("main(): Failed to set context classloader !", e);
}
In the previous excerpt I get the ClassLoader that loaded my AppBootstrap class and use it as the parent classloader of my JwsUrlFixerClassLoader.
I had to fix the problem of the default parent delegation strategy of the URLClassLodaer.loadClass() and replace it with the "try my classpath first then parent".
After that has been done everything went right and a couple of other bugs that we so far couldn't explain have disapeared.
That's magic ! After a lot of pain though...
Hope this can help someone one day...