我正在尝试从此处编译代码:http :
//www.brackeen.com/javagamebook/#download(第6章),但遇到了麻烦。我不了解如何java.util.logging.Logger
与log4j一起工作,但这似乎是问题所在。我得到的错误全部在log.error()
或log.warn()
方法调用上。
这是NetBeans的输出:
在里面:
清洁
删除目录C:\ JB \ NetBeansProjects \ WRServer \ build
删除目录C:\ JB \ NetBeansProjects \ WRServer \ dist
清洁:
在里面:
deps-jar:
创建的目录:C:\ JB \ NetBeansProjects \ WRServer \ build \ classes
将23个源文件编译到C:\ JB \ NetBeansProjects \ WRServer \ build \ classes
C:\ JB \ NetBeansProjects \ WRServer \ src \ com \ hypefiend \ javagamebook \ server \ GameServer.java:110:找不到符号
symbol:方法错误(java.lang.String,java.lang.Exception)
位置:类java.util.logging.Logger
log.error(“初始化ServerSocket时出错”,e);
^
C:\ JB \ NetBeansProjects \ WRServer \ src \ com \ hypefiend \ javagamebook \ server \ GameServer.java:152:找不到符号
symbol:方法warn(java.lang.String)
位置:类java.util.logging.Logger
log.warn(“ serverSocket select()期间出错:” + ioe.getMessage());
^
C:\ JB \ NetBeansProjects \ WRServer \ src \ com \ hypefiend \ javagamebook \ server \ GameServer.java:155:找不到符号
symbol:方法错误(java.lang.String,java.lang.Exception)
位置:类java.util.logging.Logger
log.error(“ run()中的异常”,e);
^
C:\ JB \ NetBeansProjects \ WRServer \ src \ com \ hypefiend \ javagamebook \ server \ GameServer.java:187:找不到符号
symbol:方法错误(java.lang.String)
位置:类java.util.logging.Logger
log.error(“ gameNameHash没有游戏控制器:” + gameNameHash);
^
C:\ JB \ NetBeansProjects \ WRServer \ src \ com \ hyfifiend \ javagamebook \ server \ GameServer.java:203:找不到符号
symbol:方法错误(java.lang.String)
位置:类java.util.logging.Logger
log.error(“错误获取GameController目录”);
^
C:\ JB \ NetBeansProjects \ WRServer \ src \ com \ hyfifiend \ javagamebook \ server \ GameServer.java:223:找不到符号
symbol:方法warn(java.lang.String)
位置:类java.util.logging.Logger
log.warn(“类文件不会扩展GameController:” +文件);
^
C:\ JB \ NetBeansProjects \ WRServer \ src \ com \ hyfifiend \ javagamebook \ server \ GameServer.java:238:找不到符号
symbol:方法错误(java.lang.String,java.lang.Exception)
位置:类java.util.logging.Logger
log.error(“从文件实例化GameController时出错:” +文件,e);
^
注意:某些输入文件使用未经检查或不安全的操作。
注意:使用-Xlint:unchecked重新编译以获取详细信息。
7个错误
生成失败(总时间:0秒)
这是本书的直接代码。我还没有尝试编辑它。
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 SE 6环境中: Eclipse控制台中未显示任何内容。 l.info(“”) 及以上的作品就好了,但低于任何 罚款 只是似乎没有工作。有什么问题吗?TIA。 问题答案: 即使Logger级别设置为ALL,ConsoleHandler(记录器上的默认Handler)仍然具有INFO的默认级别。这来自 JAVA_HOME / jre / lib中 的默认logging.pr
问题内容: 这可能是一个愚蠢的问题,但是我对Java Logger有点迷茫 我在哪里可以看到日志?同样来自java.util.logging.Logger库的这句话: 在每次记录调用时,记录器最初都会根据记录器的有效日志级别对请求级别(例如SEVERE或FINE)进行廉价检查。如果请求级别低于日志级别,则日志记录调用立即返回。 通过此初始(便宜)测试后,记录器将分配一个LogRecord来描述记录
我在堆栈溢出中看到,当涉及到服务器端工业级应用程序时,在System.out.println()上使用记录器框架是处理日志记录的有效方法,而且我还发现System.out.println()是一个I/O操作,因为它需要更多的CPU使用,所以我需要知道logger.info()[这是java附带的内置类]是不是一个I/O操作,如果不是,我需要解释为什么。
问题内容: 我遇到一种情况,我想将我创建的所有日志写入文本文件。 我们正在使用java.util.logging.Logger API生成日志。 我试过了: 但是仍然只能在控制台上获取我的日志。 问题答案: 试试这个样本。这个对我有用。 在MyLogFile.log产生输出 编辑: 要删除控制台处理程序,请使用 因为ConsoleHandler已向父记录器注册,所有记录器都源自该记录器。
我需要控制日志框架的最大磁盘空间量。 例如,在log4j中,如果有如下所示的追加器,我可以很容易地估计需要多少磁盘空间: 对于maxFileSize和maxBackupIndex,我知道最多需要10x100KB。 我注意到DailyRollingFileAppender,但它不支持maxFileSize。 除了RollingFileAppender之外,是否还有其他fileappender可以实现
问题内容: 我有一个使用log4j SyslogAppender(facility = USER)的Java应用程序,我可以在端口514上使用tcpdump看到我的应用程序正在将预期的日志消息作为数据报发送,并且netstat告诉我syslogd(红色帽子)正在运行并且正在监听0.0 .0.0:514,但我看不到/ var / log / messages中发生任何日志记录。 在我的syslog.