title: NPE in Win32ShellFolder2.access when creating new JFileChooser as Local System Account in Windows 7
body: <p>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.</p>
  
  <p>I've reduced the problem to that of running the following main class as local system account (NOT THE REAL CODE)</p>
  
  <pre><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");
              }
          });
      }
  }
  </code></pre>
  
  <p>Using the following build file.</p>
  
  <pre><code>&lt;project&gt;
  
  &lt;target name="clean"&gt;
      &lt;delete dir="build"/&gt;
  &lt;/target&gt;
  
  &lt;target name="compile" depends="clean"&gt;
      &lt;mkdir dir="build/classes"/&gt;
      &lt;javac srcdir="src" destdir="build/classes"/&gt;
  &lt;/target&gt;
  
  &lt;target name="jar" depends="compile"&gt;
      &lt;mkdir dir="build/jar"/&gt;
      &lt;jar destfile="build/jar/FileChooserAsSystem.jar" basedir="build/classes"&gt;
          &lt;manifest&gt;
              &lt;attribute name="Main-Class" value="com.example.mcgr.FileChooserAsSystem"/&gt;
          &lt;/manifest&gt;
      &lt;/jar&gt;
  &lt;/target&gt;
  
  &lt;target name="run"&gt;
      &lt;java jar="build/jar/FileChooserAsSystem.jar" fork="true"/&gt;
  &lt;/target&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>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).</p>
  
  <p>If I run the above code as the system account (e.g. by installing PsTools/PsExec and running 
  <code>PsExec.exe -s -i cmd.exe</code> to start cmd as system account and then running the jar, then I get the following stack trace:</p>
  
  <pre><code> [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)
  </code></pre>
  
  <p>How can I create a JFileChooser object within a JVM that has been launched by the local system account?</p>
  
  <p>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.</p>
  
  <p>I've tried various suggestions from Google including.</p>
  
  <ul>
  <li>Passing -Dswing.disableFileChooserSpeedFix=true</li>
  <li>Passing -Duser.home=./</li>
  <li>Passing -Dtemp.dir=C:/temp</li>
  </ul>
  
  <p>... but none changed the result.</p>
  
  <p>Thanks for any help.</p>