- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试从这里编译代码:http://www.brackeen.com/javagamebook/#download (第 6 章)我遇到了麻烦。我不明白 java.util.logging.Logger
和 log4j 如何一起工作,但这似乎是问题所在。我得到的错误都在 log.error()
或 log.warn()
方法调用上。
这是 NetBeans 的输出:
init:deps-clean:Deleting directory C:\JB\NetBeansProjects\WRServer\buildDeleting directory C:\JB\NetBeansProjects\WRServer\distclean:init:deps-jar:Created dir: C:\JB\NetBeansProjects\WRServer\build\classesCompiling 23 source files to C:\JB\NetBeansProjects\WRServer\build\classesC:\JB\NetBeansProjects\WRServer\src\com\hypefiend\javagamebook\server\GameServer.java:110: cannot find symbolsymbol : method error(java.lang.String,java.lang.Exception)location: class java.util.logging.Logger log.error("error initializing ServerSocket", e); ^C:\JB\NetBeansProjects\WRServer\src\com\hypefiend\javagamebook\server\GameServer.java:152: cannot find symbolsymbol : method warn(java.lang.String)location: class java.util.logging.Logger log.warn("error during serverSocket select(): " + ioe.getMessage()); ^C:\JB\NetBeansProjects\WRServer\src\com\hypefiend\javagamebook\server\GameServer.java:155: cannot find symbolsymbol : method error(java.lang.String,java.lang.Exception)location: class java.util.logging.Logger log.error("exception in run()", e); ^C:\JB\NetBeansProjects\WRServer\src\com\hypefiend\javagamebook\server\GameServer.java:187: cannot find symbolsymbol : method error(java.lang.String)location: class java.util.logging.Logger log.error("no gamecontroller for gameNameHash: " + gameNameHash); ^C:\JB\NetBeansProjects\WRServer\src\com\hypefiend\javagamebook\server\GameServer.java:203: cannot find symbolsymbol : method error(java.lang.String)location: class java.util.logging.Logger log.error("error getting GameController directory"); ^C:\JB\NetBeansProjects\WRServer\src\com\hypefiend\javagamebook\server\GameServer.java:223: cannot find symbolsymbol : method warn(java.lang.String)location: class java.util.logging.Logger log.warn("class file does not extend GameController: " + file); ^C:\JB\NetBeansProjects\WRServer\src\com\hypefiend\javagamebook\server\GameServer.java:238: cannot find symbolsymbol : method error(java.lang.String,java.lang.Exception)location: class java.util.logging.Logger log.error("Error instantiating GameController from file: " + file, e); ^Note: Some input files use unchecked or unsafe operations.Note: Recompile with -Xlint:unchecked for details.7 errorsBUILD FAILED (total time: 0 seconds)
Here is the code straight from the book. I have not tried to edit it yet.
package com.hypefiend.javagamebook.server;
import com.hypefiend.javagamebook.common.*;
import com.hypefiend.javagamebook.server.controller.*;
import java.nio.channels.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.util.logging.Logger;
import org.apache.log4j.*;
/**
* GameServer.java
*
* The heart of the framework, GameServer accepts
* incoming client connections and hands them off to
* the SelectAndRead class.
* GameServer also keeps track of the connected players
* and the GameControllers.
*
* @author <a href="mailto:bret@hypefiend.com">bret barker</a>
* @version 1.0
*/
public class GameServer extends Thread {
/** log4j Logger */
private Logger log = Logger.getLogger("GameServer");
/** ServerSocketChannel for accepting client connections */
private ServerSocketChannel sSockChan;
/** selector for multiplexing ServerSocketChannels */
private Selector selector;
/** GameControllers keyed by GameName */
private Hashtable gameControllers;
/** classname prefix used for dynamically loading GameControllers */
private static final String CONTROLLER_CLASS_PREFIX =
"com.hypefiend.javagamebook.server.controller.";
/** players keyed by playerId */
private static Hashtable playersByPlayerId;
/** players keyed by sessionId */
private static Hashtable playersBySessionId;
private boolean running;
private SelectAndRead selectAndRead;
private EventWriter eventWriter;
private static long nextSessionId = 0;
/**
* main.
* setup log4j and fireup the GameServer
*/
public static void main(String args[]) {
BasicConfigurator.configure();
GameServer gs = new GameServer();
gs.start();
}
/**
* constructor, just initialize our hashtables
*/
public GameServer() {
gameControllers = new Hashtable();
playersByPlayerId = new Hashtable();
playersBySessionId = new Hashtable();
}
/**
* init the GameServer, startup our workers, etc.
*/
public void init() {
log.info("GameServer initializing");
loadGameControllers();
initServerSocket();
selectAndRead = new SelectAndRead(this);
selectAndRead.start();
eventWriter = new EventWriter(this, Globals.EVENT_WRITER_WORKERS);
}
/**
* GameServer specific initialization, bind to the server port,
* setup the Selector, etc.
*/
private void initServerSocket() {
try {
// open a non-blocking server socket channel
sSockChan = ServerSocketChannel.open();
sSockChan.configureBlocking(false);
// bind to localhost on designated port
InetAddress addr = InetAddress.getLocalHost();
log.info("binding to address: " + addr.getHostAddress());
sSockChan.socket().bind(new InetSocketAddress(addr, Globals.PORT));
// get a selector
selector = Selector.open();
// register the channel with the selector to handle accepts
SelectionKey acceptKey = sSockChan.register(selector, SelectionKey.OP_ACCEPT);
}
catch (Exception e) {
log.error("error initializing ServerSocket", e);
System.exit(1);
}
}
/**
* Here's the meat, loop over the select() call to
* accept socket connections and hand them off to SelectAndRead
*/
public void run() {
init();
log.info("******** GameServer running ********");
running = true;
int numReady = 0;
while (running) {
// note, since we only have one ServerSocket to listen to,
// we don't need a Selector here, but we set it up for
// later additions such as listening on another port
// for administrative uses.
try {
// blocking select, will return when we get a new connection
selector.select();
// fetch the keys
Set readyKeys = selector.selectedKeys();
// run through the keys and process
Iterator i = readyKeys.iterator();
while (i.hasNext()) {
SelectionKey key = (SelectionKey) i.next();
i.remove();
ServerSocketChannel ssChannel = (ServerSocketChannel) key.channel();
SocketChannel clientChannel = ssChannel.accept();
// add to the list in SelectAndRead for processing
selectAndRead.addNewClient(clientChannel);
log.info("got connection from: " + clientChannel.socket().getInetAddress());
}
}
catch (IOException ioe) {
log.warn("error during serverSocket select(): " + ioe.getMessage());
}
catch (Exception e) {
log.error("exception in run()", e);
}
}
}
/**
* shutdown the GameServer
*/
public void shutdown() {
selector.wakeup();
}
/**
* Return the next available sessionId
*/
public synchronized String nextSessionId() {
return "" + nextSessionId++;
}
/**
* finds the GameController for a given GameName
*/
public GameController getGameController(String gameName) {
return getGameControllerByHash(gameName.hashCode());
}
/**
* finds the GameController for a given GameName hash code
*/
public GameController getGameControllerByHash(int gameNameHash) {
GameController gc = (GameController) gameControllers.get("" + gameNameHash);
if (gc == null)
log.error("no gamecontroller for gameNameHash: " + gameNameHash);
return gc;
}
/**
* Dynamically loads GameControllers
*/
private void loadGameControllers() {
log.info("loading GameControllers");
// grab all class files in the same directory as GameController
String baseClass = "com/hypefiend/javagamebook/server/controller/GameController.class";
File f = new File( this.getClass( ).getClassLoader().getResource(baseClass).getPath());
File[] files = f.getParentFile().listFiles( );
if (files == null) {
log.error("error getting GameController directory");
return;
}
for( int i = 0; ( i < files.length); i++) {
String file = files[i].getName( );
if (file.indexOf( ".class") == -1)
continue;
if (file.equals("GameController.class"))
continue;
try {
// grab the class
String controllerClassName = CONTROLLER_CLASS_PREFIX + file.substring(0, file.indexOf(".class"));
log.info("loading class: " + controllerClassName);
Class cl = Class.forName(controllerClassName);
// make sure it extends GameController
if (!GameController.class.isAssignableFrom(cl)) {
log.warn("class file does not extend GameController: " + file);
continue;
}
// get an instance and initialize
GameController gc = (GameController) cl.newInstance();
String gameName = gc.getGameName();
gc.init(this, getGameConfig(gameName));
// add to our controllers hash
gameControllers.put("" + gameName.hashCode(), gc);
log.info("loaded controller for gameName: " + gameName + ", hash: " + gameName.hashCode());
}
catch (Exception e) {
log.error("Error instantiating GameController from file: " + file, e);
}
}
}
/**
* pass the event on to the EventWriter
*/
public void writeEvent(GameEvent e) {
eventWriter.handleEvent(e);
}
/**
* returns the GameConfig object for the given gameName
*/
public GameConfig getGameConfig(String gameName) {
// todo: implement getGameConfig()
return null;
}
/**
* fetches the Player for a given playerId
*/
public static Player getPlayerById( String id) {
return (Player) playersByPlayerId.get(id);
}
/**
* fetches the Player for a given sessionId
*/
public static Player getPlayerBySessionId(String id) {
return (Player) playersBySessionId.get(id);
}
/**
* add a player to our lists
*/
public static void addPlayer(Player p) {
playersByPlayerId.put(p.getPlayerId(), p);
playersBySessionId.put(p.getSessionId(), p);
}
/**
* remove a player from our lists
*/
public static void removePlayer(Player p) {
playersByPlayerId.remove(p.getPlayerId());
playersBySessionId.remove(p.getPlayerId());
}
}// GameServer
最佳答案
在您的类中导入 java.util.logging.Logger 似乎会导致问题(编译器会尝试针对该类进行编译,尽管其意图似乎是使用 log4j Logger 类)。
尝试从导入中删除 java.util.logging.Logger 并重新编译。
编辑:好吧,我刚刚检查了问题链接页面中链接的 ZIP 文件中的原始 GameServer.java。它不包含任何 java.util.logging.Logger 的导入。因此我的猜测是:
因此,首先将 log4j 添加到类路径,然后从导入中删除 java.util.logging.Logger。
关于java.util.logging.Logger 和 log4j,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/582428/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!