问题描述
我在将其部署到运行JRE 1.7的其他计算机时崩溃的应用程序遇到了麻烦.当我在PC上的NetBean(甚至直接从JAR文件中)运行此操作时,一切都很好.但是在另一台计算机上,在执行过程中,它在特定事件(按钮点击)中失败.
所以,我了解了使用Log4J库来记录的.这为我提供了有关应用程序中问题的一些信息,并且记录又可以在我的计算机上完美效果.但是,当我将JAR文件部署到其他仅运行JRE的计算机(Java 7 Update 17)时,我找不到任何日志文件的痕迹.
这是我的log4j.properties文件:
# Root logger option log4j.rootLogger=INFO, file, stdout # Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\logging.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
在我的计算机上,我可以在项目文件夹内部看到logging.log文件.在某种程度上,一切正常.但是,在用户PC上,根本没有此文件的迹象.不在c:\中(我认为会在哪里),而不是在c:\ program文件(x86)\或其他任何地方.我已经对硬盘进行了完整的搜索,但是什么都没有.
应该存储此文件的位置?我的属性设置正确吗?非常困惑...
谢谢!
推荐答案
如果任何人都偶然发现这篇文章,我想记录我如何解决它.
首先,正如DWB正确指出的那样,问题确实在于,用户的帐户没有足够的特权来创建logging.log文件.在我的代码中,我有一个正在执行的捕获块,并包含代码在文件写入错误时退出系统.由于这正是我要编写的记录细节,所以我无法吸引任何输出来向我提出问题的来源.
一旦我意识到这一点,我就必须更改编写日志文件的地方.与其将其写入根(c:\)或jar文件为(c:\ program files \)的文件夹,这两个地方都是我不能保证用户拥有完全特权的地方在其AppData路径中创建一个文件夹.
我程序中的第一行是对此功能的调用(使用通用名称myprogram):
private static void makeAppDataFolder() { File dir = new File(System.getenv("APPDATA") + "\\MyProgram"); if (!dir.exists()) {dir.mkdir();} }
这将在\ appdata \ roaming上在用户帐户中创建一个称为myprogram的文件夹.由于此路径是用户帐户路径的一部分,因此我相信用户将始终能够写入它,从而解决权限问题.如果有人知道,请告诉我.
然后,在我的log4j.properties文件中,我更改了行
log4j.appender.file.File=logging.log
to
log4j.appender.file.File=${user.home}/Application Data/MyProgram/logging.log
这将文件引导到我刚创建的文件夹.
我这样做并添加了一些放置的记录消息,我就能找到其他问题,现在一切正常.
我希望这对某人有帮助.如果有人有任何建议,请发布.
问题描述
I'm having trouble with an application that crashes when I deploy it to other computers who are running JRE 1.7. When I run this inside of NetBeans (or even directly from the JAR file) on my PC, everything is fine. But on another computer it fails at specific events (button clicks) during execution.
So, I learned about logging using the log4j library. This gave me some information on a problem in my application, and the logging works perfectly, again on MY computer. But when I deploy the JAR file to other computers, who are only running JRE (Java 7 Update 17), I can find no traces of any log files.
Here is my log4j.properties file:
# Root logger option log4j.rootLogger=INFO, file, stdout # Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\logging.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
On my computer, I can see the logging.log file right inside of the project folder. To that extent, everything works perfectly. However, on the user PC, there is no sign of this file at all. Not in C:\ (where I thought it would be), not in C:\Program Files (x86)\ or anywhere else. I've done a complete search of my hard disk, but nothing comes back.
Where should this file be stored? Are my properties set correctly? Very confused...
Thank you!
推荐答案
In case anybody ever stumbles on this post, I wanted to document how I solved it.
First of all, as DwB correctly pointed out, the problem was indeed that the user's account did not have sufficient privileges to create the logging.log file. In my code, I had a catch block that was being executed and contained code to exit the system in the event of a file write error. Since it was exactly the logging detail was what I was trying to write, I was unable to get any output to clue me into this as the source of my problem.
Once I realized this, I just had to change where I was writing the log file. Rather than write it to the root (C:\) or to the folder where the jar file was (C:\Program Files\), both of which were places where I could not guarantee that a user would have full privileges, I decided to create a folder in their AppData path.
The very first line in my program is now a call to this function (using the generic name MyProgram):
private static void makeAppDataFolder() { File dir = new File(System.getenv("APPDATA") + "\\MyProgram"); if (!dir.exists()) {dir.mkdir();} }
This creates a folder called MyProgram in the user's account at \AppData\Roaming. Since this path is part of the user account path, I believe that the user will always be able to write to it, thereby solving the permissions issue. If anybody knows otherwise, please let me know.
Then, in my log4j.properties file, I change the line
log4j.appender.file.File=logging.log
to
log4j.appender.file.File=${user.home}/Application Data/MyProgram/logging.log
This directs the file to the folder that I just created.
Once I did this, and added some well-placed logging messages, I was able to find my other issues, and now everything works just fine.
I hope that this helps somebody out. If anyone has any suggestions, please post.