在Windows 7中创建新的JFileChooser作为本地系统帐户时,Win32ShellFolder2.access中的NPE [英] NPE in Win32ShellFolder2.access when creating new JFileChooser as Local System Account in Windows 7
问题描述
我已经为创建JFileChooser的Swing GUI编写了单元测试.由于单元测试作为服务在构建服务器上运行,因此单元测试需要作为本地系统帐户运行.但是,当单元测试尝试创建新的JFileChooser时,它们会抛出NullPointerException.
I have written unit tests for a Swing GUI that creates JFileChooser. Since the unit tests are run on a build server as a service, the unit tests need to run as the local system account. However, when the unit tests try to create a new JFileChooser, they throw a NullPointerException.
我已将问题简化为以本地系统帐户身份运行以下主类的问题(不是真实代码)
I've reduced the problem to that of running the following main class as local system account (NOT THE REAL CODE)
package com.example.mcgr;
import javax.swing.*;
import java.io.IOException;
public class FileChooserAsSystem {
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.showDialog(null, "Ok");
}
});
}
}
使用以下构建文件.
<project>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/FileChooserAsSystem.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="com.example.mcgr.FileChooserAsSystem"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/FileChooserAsSystem.jar" fork="true"/>
</target>
</project>
如果我以自己的用户帐户运行代码,则会出现JFileChooser(这就是我想要的,因为上面精简的代码显然在那之后什么也没做.)
If I run the code as my own user account, the JFileChooser appears (that's all I want it to do as the above stripped down code obviously doesn't do anything after that).
如果我以系统帐户身份运行上述代码(例如,通过安装PsTools/PsExec并运行
PsExec.exe -s -i cmd.exe
以系统帐户身份启动cmd,然后运行jar,然后得到以下堆栈跟踪:
If I run the above code as the system account (e.g. by installing PsTools/PsExec and running
PsExec.exe -s -i cmd.exe
to start cmd as system account and then running the jar, then I get the following stack trace:
[java] Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
[java] at sun.awt.shell.Win32ShellFolder2.access$200(Win32ShellFolder2.java:72)
[java] at sun.awt.shell.Win32ShellFolder2$1.call(Win32ShellFolder2.java:242)
[java] at sun.awt.shell.Win32ShellFolder2$1.call(Win32ShellFolder2.java:237)
[java] at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
[java] at java.util.concurrent.FutureTask.run(FutureTask.java:166)
[java] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
[java] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
[java] at sun.awt.shell.Win32ShellFolderManager2$ComInvoker$3.run(Win32ShellFolderManager2.java:502)
[java] at java.lang.Thread.run(Thread.java:724)
如何在由本地系统帐户启动的JVM中创建JFileChooser对象?
How can I create a JFileChooser object within a JVM that has been launched by the local system account?
我当前使用的是JVM 1.7.0_25 32位版本,并且已经在Windows Server 2008和Windows 7上进行了测试.还有另一个要求,这意味着我无法从32位JVM切换到64位JVM.
I'm currently using JVM version 1.7.0_25 32bit and have tested on both Windows Server 2008 and Windows 7. There's another requirement that means I can't switch from a 32bit JVM to 64bit JVM.
我尝试了Google的各种建议,包括.
I've tried various suggestions from Google including.
- 传递-Dswing.disableFileChooserSpeedFix = true
- 传递-Duser.home =./
- 传递-Dtemp.dir = C:/temp
...但是没有改变结果.
... but none changed the result.
感谢您的帮助.
推荐答案
这不是JVM问题,而是权限问题.我最近遇到了在Windows 10计算机上运行Java 8v92的类似堆栈跟踪.
This is not a JVM issue but a permissions issue. I recently encountered a similar stack trace running Java 8v92 on a Windows 10 machine.
要解决此问题,请将Windows服务设置为以管理员帐户登录(启动服务",突出显示该服务并显示属性",然后在Windows 10中选择登录"选项卡):
To fix this, set the Windows Service to log on as an Administrator account (launch Services, highlight the service and show Properties, select Log On tab in Windows 10):
这篇关于在Windows 7中创建新的JFileChooser作为本地系统帐户时,Win32ShellFolder2.access中的NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!