此代码:
private BasePlayer addPlayer(Player player) {
BasePlayer currentPlayer;
try{
String playerClassName = this.getClass().getPackage().toString() + ".Player";
Class cPlayer = Class.forName(playerClassName);
Constructor constructor = cPlayer.getConstructor(new Class[]{ Player.class });
currentPlayer = (BasePlayer) constructor.newInstance(new Object[]{player});
} catch (ClassNotFoundException e) {
log.info(e.toString());
currentPlayer = new BasePlayer(player);
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException e) {
log.info(e.toString());
return null;
}
log.info(player.getName() + " added to player list with UUID: " + player.getUniqueId().toString());
playerMap.put(player.getUniqueId(), currentPlayer);
return currentPlayer;
}
引发异常
Java.lang.ClassNotFoundException:包com.warnea.codecraft.Player
但是如果我改变
String playerClassName = this.getClass().getPackage().toString() + ".Player";
Class cPlayer = Class.forName(playerClassName);
到
String playerClassName = "com.warnea.codecraft.Player";
Class cPlayer = Class.forName(playerClassName);
它工作正常。为什么我似乎需要对软件包名称进行硬编码?