title: What's the best way to start Java applications on Windows 7?
body: <h1>Requirements</h1>
  
  <p>I want to publish a Java GUI application on Windows 7. This application is using the Swing Toolkit and doesn't need any native code. The application is installed using an <a href="http://nsis.sourceforge.net/Main_Page" rel="nofollow noreferrer">NSIS</a> installer. I want to integrate this application into Windows 7 as good as possible. This means:</p>
  
  <ul>
  <li>When the application is running it must be possible to pin the application to the taskbar.</li>
  <li>It must be possible to associate data files with the application so Windows opens these files with my application.</li>
  <li>Must automatically work with the 32 bit Java Runtime and with the 64 bit Java runtime. So when the user uninstalls a 32 bit Java and installs a 64 bit Java instead (Or vice-versa) then my application must still work.</li>
  <li>Must support large fonts setting of Windows. I don't really understand this feature. I just know that some applications ignore it completely, others (Like Google Chrome) are pixel-scaled (Looks really ugly) and others support it by simply using larger fonts as intended (That's what I want and normally it works. Only the WinRun4J solution mentioned below doesn't work with it).</li>
  </ul>
  
  <h1>Tested solutions</h1>
  
  <h2>WinRun4J</h2>
  
  <p><a href="http://winrun4j.sourceforge.net/" rel="nofollow noreferrer">WinRun4j</a> is an EXE file which launches the Java application. Because the application doesn't fork a new Java process Windows thinks the EXE file IS the application. So there is no problem with the taskbar. File associations works because the files can be simply associated with the EXE file.</p>
  
  <p><strong>Problems:</strong></p>
  
  <ul>
  <li>Doesn't support large fonts. Application window is pixel-scaled instead (Like Google Chrome).</li>
  <li>Two different EXE files must be used depending on the installed JRE. So when the 64 bit JRE is installed then the application must be started with the 64 Bit EXE file. When 32 bit JRE ins installed then the other EXE must be used. This isn't user-friendly because the user doesn't understand why he must use the 32 bit EXE on a 64 bit operating system when only a 32 bit JRE is installed.</li>
  </ul>
  
  <h2>Launch4J</h2>
  
  <p><a href="http://launch4j.sourceforge.net/" rel="nofollow noreferrer">Launch4J</a> creates a 32 bit EXE which launches an external Java process to start the Java application. So unlike WinRun4J it can also start a 64 bit Java.</p>
  
  <p><strong>Problems:</strong></p>
  
  <ul>
  <li>Can't pin the application to the taskbar.</li>
  <li><code>System.out.println</code> will not print to console if <code>headerType="gui"</code>, regardless if application is started from console.</li>
  </ul>
  
  <h2>JAR</h2>
  
  <p>On Windows you can simply double click the JAR file to start the application. Installed JRE doesn't matter, simply works. But...</p>
  
  <p><strong>Problems:</strong></p>
  
  <ul>
  <li>Application can't be pinned to the taskbar.</li>
  <li>Can't create a shortcut in the start menu.</li>
  <li>Can't associate files with a JAR file.</li>
  </ul>
  
  <h2>BAT/CMD</h2>
  
  <p>A simple batch file like this can be used to start the application:</p>
  
  <pre><code>@echo off
  start c:\windows\system32\javaw.exe -jar "c:\program files\myapp\myapp.jar" %1
  </code></pre>
  
  <p>A shortcut can be created for this batch file to set a custom icon.</p>
  
  <p><strong>Problems:</strong></p>
  
  <ul>
  <li>A DOS window pops up when the application is started.</li>
  <li>The batch file doesn't know where javaw.exe is located. Depending on which java version (32 or 64 bit) is installed it may be located in <code>c:\windows\syswow64</code> instead and Windows doesn't redirect this call from batch files automatically. Using the <code>JAVA_HOME</code> environment variable is also a no-go because Java doesn't set this automatically.</li>
  <li>When associating files with the batch file then no custom icon can be set.</li>
  <li>Taskbar support isn't working properly. Application can be pinned to it when the batch file is started manually but when double clicking an associated file instead then it doesn' work.</li>
  </ul>
  
  <h2>Shortcut</h2>
  
  <p>Instead of using a batch file it is possible to only create a shortcut to start the application. It links to this command: <code>c:\windows\system32\javaw.exe -jar "c:\program files\myapp\myapp.jar"</code>. Windows automatically redirects this call to the SysWOW64 directory if a 32 bit Java JRE is installed.</p>
  
  <p><strong>Problems:</strong></p>
  
  <ul>
  <li>Not possible to associate files with it because Windows only accepts EXE/COM/PIF/BAT/CMD files as association targets. LNK files don't work.</li>
  </ul>
  
  <h1>Question</h1>
  
  <p>Is there another solution which fulfills all the requirements from above? Or are there any tricks to solve the problems with the mentioned solutions? </p>
  
  <h1>Solution</h1>
  
  <p>After solving the taskbar-pinning problem using <a href="http://launch4j.sourceforge.net/" rel="nofollow noreferrer">Launch4j</a> looks like the best solution. Launch4j can easily be integrated into a Maven project (With <a href="http://9stmaryrd.com/tools/launch4j-maven-plugin/" rel="nofollow noreferrer">this</a> or <a href="http://alakai.org/reference/plugins/launch4j-plugin-usage.html" rel="nofollow noreferrer">this</a> plugin), configuration is pretty easy and everything works out of the box except taskbar pinning. For taskbar-pinning the Java application must set an appModelUserId as explained in the answer to <a href="https://stackoverflow.com/questions/1907735/using-jna-to-get-set-application-identifier">this question</a>.</p>
  
  <p>Additionally the Java application must be installed by an installer which must at least install one shortcut pointing to the EXE. This shortcut must also contain the appModelUserId. With NSIS this can be done with the <a href="http://nsis.sourceforge.net/WinShell_plug-in" rel="nofollow noreferrer">WinShell plugin</a> and a configuration like this:</p>
  
  <pre><code>CreateShortCut "$SMPROGRAMS\MyApp.lnk" \
      "$INSTDIR\myapp.exe" "" "$INSTDIR\myapp.exe" 0 SW_SHOWNORMAL
  WinShell::SetLnkAUMI "$SMPrograms\MyApp.lnk" "MyAppModelUserId"
  </code></pre>
  
  <p>For some unknown reason this shortcut only has to exist. You don't have to use it. You can double-click the EXE and taskbar-pinning still works. You can even create the shortcut in some subfolder of your application folder. Taskbar-pinning stops working when the last shortcut of the EXE file is removed.</p>