What's the best way to start Java applications on Windows 7?

21,405

Solution 1

Try Launch4j (http://launch4j.sourceforge.net/), its a simple jar to exe wrapper (actually wrapping the jar is optional). It should solve your Icon and Taskbar requirements. Its also capable locating installed JRE's (some configurable rules). The font problem I don't quite get, Swing should automatically use fonts depending on Windows settings, unless you somehow overwrite that in the JRE options or in code.

Solution 2

Java Web Start - I wouldn't consider distributing an application any other way, these days.

The user does need to have at least J2SE 1.4; if your applications needs a later version, Web Start will automatically download an appropriate JRE.

See the JNLP reference for the tags for desktop integration (shortcut and offline-allowed), and file associations (association). These are only supported in WS 1.5 though.

Solution 3

I personaly use Launch4j (through maven with the maven-launch4j-plugin to be even more precise), and I implement the system tray management from within my application... (See http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/).

Solution 4

I've had good success with WinRun4J overall, but I haven't really done much with fonts, so I'll confess I'm not sure I understand why you're having the issue you describe there.

From what you describe, however, it sounds like you have very specific requirements from a Java native launcher. Why not just write your own? You could start with something like WinRun4J (which is open source, licensed under the Eclipse CPL) and just modify it to your needs.

Alternatively, you could look into the native launchers used by other programs. The Eclipse and NetBeans launchers both seem to work pretty well, and both are open source. You might be able to adapt one of them pretty easily as well.

Solution 5

As an aside, also check out the new feature to have your app in the tooltray/systemtray:

Oracle has a tutorial on how to use the system tray.

That's Java SE 6 related....makes me also winder what other goodies might be in the newer Java 7?

Share:
21,405
kayahr
Author by

kayahr

„People assume that a Closure is a function having access to the parent scope, even after the parent function has closed, but actually from a non-linear, non-subjective viewpoint - it's more like a big ball of wibbly wobbly... timey wimey... stuff.“ – The JavaScript Doctor

Updated on March 14, 2020

Comments

  • kayahr
    kayahr about 4 years

    Requirements

    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 NSIS installer. I want to integrate this application into Windows 7 as good as possible. This means:

    • When the application is running it must be possible to pin the application to the taskbar.
    • It must be possible to associate data files with the application so Windows opens these files with my application.
    • 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.
    • 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).

    Tested solutions

    WinRun4J

    WinRun4j 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.

    Problems:

    • Doesn't support large fonts. Application window is pixel-scaled instead (Like Google Chrome).
    • 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.

    Launch4J

    Launch4J 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.

    Problems:

    • Can't pin the application to the taskbar.
    • System.out.println will not print to console if headerType="gui", regardless if application is started from console.

    JAR

    On Windows you can simply double click the JAR file to start the application. Installed JRE doesn't matter, simply works. But...

    Problems:

    • Application can't be pinned to the taskbar.
    • Can't create a shortcut in the start menu.
    • Can't associate files with a JAR file.

    BAT/CMD

    A simple batch file like this can be used to start the application:

    @echo off
    start c:\windows\system32\javaw.exe -jar "c:\program files\myapp\myapp.jar" %1
    

    A shortcut can be created for this batch file to set a custom icon.

    Problems:

    • A DOS window pops up when the application is started.
    • 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 c:\windows\syswow64 instead and Windows doesn't redirect this call from batch files automatically. Using the JAVA_HOME environment variable is also a no-go because Java doesn't set this automatically.
    • When associating files with the batch file then no custom icon can be set.
    • 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.

    Shortcut

    Instead of using a batch file it is possible to only create a shortcut to start the application. It links to this command: c:\windows\system32\javaw.exe -jar "c:\program files\myapp\myapp.jar". Windows automatically redirects this call to the SysWOW64 directory if a 32 bit Java JRE is installed.

    Problems:

    • 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.

    Question

    Is there another solution which fulfills all the requirements from above? Or are there any tricks to solve the problems with the mentioned solutions?

    Solution

    After solving the taskbar-pinning problem using Launch4j looks like the best solution. Launch4j can easily be integrated into a Maven project (With this or this 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 this question.

    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 WinShell plugin and a configuration like this:

    CreateShortCut "$SMPROGRAMS\MyApp.lnk" \
        "$INSTDIR\myapp.exe" "" "$INSTDIR\myapp.exe" 0 SW_SHOWNORMAL
    WinShell::SetLnkAUMI "$SMPrograms\MyApp.lnk" "MyAppModelUserId"
    

    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.

  • kayahr
    kayahr over 12 years
    Can you provide a link to some nice application which is distributed like this so I can easily check how it feels? What happens when the User has not installed Java yet? An installer can download and install a JRE automatically if needed but how does it work with WebStart?
  • nIcE cOw
    nIcE cOw over 12 years
    @kayahr : Hope Wikipedia can help you on that. Check out Well Known Applications. More Info about Web Start
  • assylias
    assylias over 12 years
    @kayahr The Java Tutorial is full of Java Web Start examples, like here: docs.oracle.com/javase/tutorial/ui/overview/demo.html
  • kayahr
    kayahr over 12 years
    Yes, Java itself has no problem with large fonts. The fonts are nicely scaled up, enlarging the whole layout and the Swing application still works perfectly. But when starting the application with WinRun4J then windows seems to scale the output of the Swing frame as a bitmap. So it looks like you are running a 1280x800 desktop on a 1650x1050 monitor. Everything is pixel-scaled (With anti-aliasing).
  • kayahr
    kayahr over 12 years
    I just tried Launch4J. Can't pin the application to the taskbar. Doesn't matter if I wrap the JAR or not.
  • kayahr
    kayahr over 12 years
    But what about the requirements: Is it possible to pin a Web Start application to the taskbar? And can you associate a file extension with the application so you can start the application by double-clicking a data file? I don't think so. But maybe I'm wrong?
  • Durandal
    Durandal over 12 years
    There's an option "Custom process name and XP style mainfest" (on the first tab), I'm assuming that should do the trick.
  • Dmitri
    Dmitri over 12 years
    @kayahr: File associations are supported, see my edit. Afraid I don't know how it behaves when pinned (quick googling suggests it may have problems). I'm not on Windows 7 - why not try it with one of the example apps?
  • kayahr
    kayahr over 12 years
    @Dmitri: Tried it. Can't pin it to the taskbar. Dragging the JNLP file into it also doesn't work because this pins the Web Start Launcher itself to the taskbar instead.
  • Durandal
    Durandal over 12 years
    This blog entry claims it depends on the .EXE name, cant try this out because we have no Win7 here: west-wind.com/weblog/posts/2009/Oct/08/…
  • Dmitri
    Dmitri over 12 years
    Can you pin the shortcut that's created if you use desktop integration?
  • kayahr
    kayahr over 12 years
    I'm pretty sure my application name isn't a reserved name. But I tried renaming it to some random wkjbskdfjbnwer.exe, doesn't help :-)
  • kayahr
    kayahr over 12 years
    @Dmitri: Is this "Desktop Integration" something the application must provide/enable? If yes: Any hints which of the many demo applications uses it? I don't see any special integration stuff in the demos I've seen so far.
  • Dmitri
    Dmitri over 12 years
    @kayahr: It's an option in the JNLP file (check the spec linked above). Try Bloom for an example, it will create a desktop shortcut as part of the install - see if you can pin that.
  • kayahr
    kayahr over 12 years
    @Dmitri: Ok, I tried Bloom. It created a Desktop shortcut but the application can't be pinned to the taskbar. I can drag the shortcut into the taskbar but when I click the icon there then the application is started with a second icon. So no luck here unfortunately.
  • Dmitri
    Dmitri over 12 years
    Oh well. Though I'm starting to get the impression that it's the Win7 taskbar implementation that's broken.
  • Voo
    Voo over 12 years
    @Dmitri Well it certainly is possible - see IntelliJ or eclipse for examples that work fine with pinning.. no idea how they're doing it though. But asking on the eclipse mailing list or looking at their code could help (and there's certainly no weird scaling going on - eclipse looks as fine on my 24" as 30" monitor)
  • kayahr
    kayahr over 12 years
    The tray is not in my list requirements. I used it before and it works (except some transparency problems with the icon). It has nothing to do with the way the java application is launched. It always works the same.
  • kayahr
    kayahr over 12 years
    Using the Tray is not a requirement of a good integration into Windows. The tray should only be used for notifications and unfortunately it is misused by many applications for other purposes. My application has no need for notifications so I don't need the Tray. But when I need it then using it is no problem and has nothing to do with the way how I launch the application.
  • Yanflea
    Yanflea over 12 years
    Definitely...Sorry. When you spoke about the pinning issue on the taskbar, I thought you were actually refering to the system tray ... (I am not a Windows 7 user... :).
  • kayahr
    kayahr over 12 years
    I changed "Windows" to "Windows 7" in the question text to make it more clear. On other Windows versions "pinning" is not an issue because it is not supported anyway. And the other requirements are the same on all Windows versions.
  • kayahr
    kayahr over 12 years
    I tried Eclipse and Netbeans on Windows. Result: They have the same problem I'm trying to solve: They can't be pinned to the taskbar. Right click on the icon only shows "Close application", no pinning. Dragging the launcher icons into the taskbar manually works but when clicking these launchers then a second icon spawns in the taskbar.
  • kayahr
    kayahr over 12 years
    @Voo I tried Eclipse on Windows 7. It DOESN'T support pinning. Right-clicking the icon in the task bar only shows "Close application" and when I manually drag the launcher into the taskbar and click it then a second icon spawns in the taskbar.
  • Voo
    Voo over 12 years
    @kayahr You have to edit the eclipse ini file and add the JVM to it (no idea why). Works perfectly fine on my win7 install
  • kayahr
    kayahr over 12 years
    @Voo Searched for this solution and found it. But it didn't work initially. I had no context-menu on the launcher icon at all (Even no "Close Window") Then I noticed this requirement: "Make sure eclipse does not run from a path with spaces in the directory name". Well, I installed Eclipse into "c:\Program Files\Eclipse". When I move it to "c:\Eclipse" then it works. But honestly: This isn't a nice solution. I want to install my applicaiton properly with an installer and this means the application HAS to go into C:\Program Files".
  • Voo
    Voo over 12 years
    @kayahr Hum that's strange. My install is in C:\Program Files (x86)\eclipse and it works just fine. The one thing I remember struggling with was that the ini file has to look like -vm \NL path - without the newline it just stops working. Seems to me all these things are idiosyncrasies of eclipse - intelliJ works just fine without me ever changing the ini file or anything.
  • kayahr
    kayahr over 12 years
    @Voo You are right, Intellij works fine with the Taskbar. But as far as I know it is not open source so I can't see how they do it. There are lots of DLLs and EXE files in the bin folder of Intellij so I guess their solution is VERY Intellij-specific.
  • kayahr
    kayahr over 12 years
    So far Launch4j is my favorite solution and I would accept this answer when I could solve the one-and-only-problem with it. I created a follow-up question to find a solution for this specific problem.
  • kayahr
    kayahr about 12 years
    Taskbar-pinning problem is solved (I explained it at the bottom of the question). I accept this answer because Launch4j now fulfills all the requirements mentioned in the question.
  • Rekin
    Rekin over 10 years
    It's possible to Pin a WebStart application. One has only tell Windows about how to launch your application. The "java" and "javaw" executables are configured as "host" processes in Windows and thus some application cooperation is required. The easiest way to achieve it is when some exe wrapper (launcher) to host JVM is used: One native call to setCurrentProcessExplicitAppUserModelID will suffice. However, if no wrapping process exist, one can set the required options (AppUserModelID and RelaunchCommand) on the Window instance. It's very easy using the J7Goodies library. JNI/JNA work as well.
  • Rekin
    Rekin over 10 years
    That's because the processes don't set AppUserModelID.
  • Rekin
    Rekin over 10 years
    If I understand correctly, the solution here works more by accident then by design. The Windows TaskBar manages it's elements based on AppUserModelID values (which can be set on a process or on a Window HWND instance). It seems some heuristics are used, when no explicit value exist: as if it were guessed based on the installed shortcut. The shortcut is not really necessary. For the wrapped exe scenario calling the setCurrentProcessExplicitAppUserModelID function (from Shell32.dll) would make the taskbar icon pinnable.