Pinning a Java executable (with launch4j) to the Windows 7 taskbar - java

After spending half a day searching and trying, I'm finally giving up.
I have a java application of which I create a runnable jar (to include any other libs and just have a single jar file). With launch4j and the runnable jar I'm making an executable "MyApp.exe".
The executable ist working fine, but I want to pin it to my windows 7 taskbar. For now, I just have the taskbar entry "Close window".
After reading and implementing the following solutions with JNA
Pinning a Java application to the Windows 7 taskbar
Using JNA to get/set application identifier
my Application displays it's "Application User Model ID" correctly in the gui (just for testing purpose).
BUT: my program is shown as "javaw.exe" in the Task Manager and I still can't pin it to the taskbar, even though I set the launch4j option "custom process name and XP style manifest".
Background information: I'm working with a windows 7 admin account and I don't want the app the require admin rights.
Anyways, if I start the app "as administrator" from the context menu and confirm the UAC message, I can now pin to the taskbar. BUT: even though I set the "Application User Model ID" properly, windows still wants to pin "javaw.exe", even though my program is now shown as "MyApp.exe" in the TaskManager.
I'm totaly confused. But I'm obviously not the only one, having these issues.
=> See the last comments to Gregory Pakosz answer in Using JNA to get/set application identifier
Final questions:
Gregory Pakosz way with JNA to set the "Application User Model ID" ( https://stackoverflow.com/a/1928830/1128689 ) is working for me. But still, windows recognizes my app as an instance of "javaw.exe". What else do I have to do?
Did maybe some windows or java update break something here?
Do I really have to run my app with elevated user rights? I really don't want to...
Are there some more options in launch4j which I have to set?
Do I have to use a manifest file in launch4j?

I got this working by creating an Exe from a runnable jar with the help of JSmooth
.Pinned it to the taskbar, and ran it with no problems.
Hope this helps

No problem with winrun4j either, which is newer and easier to use than Jsmooth

The root cause: javaw is registered as a Host process (in Windows' registry). The shortcut behaviour is probably caused by following (source: Application User Model IDs (AppUserModelIDs))
Application-Defined and System-Defined AppUserModelIDs
Some applications do not declare an explicit AppUserModelID. They are
optional. In that case, the system uses a series of heuristics to
assign an internal AppUserModelID.
[...]
if the process was launched through a shortcut that contains launch
arguments (usually the target content to host as the "application"),
the system can determine identity and the application can be pinned
and relaunched.
About the Host Process behaviour:
Registering an Application as a Host Process
An application can set
the IsHostApp registry entry to cause that executable's process to be
considered a host process by the taskbar. This affects its grouping
and default Jump List entries.

Are you looking for this, which I used in my application to show the running Java application on windows taskbar..
SystemTray systemTray = new SystemTray(composite, parent.getShell(), parent.getDisplay(), "My Application");
systemTray.makeSystemTray();

Related

Execute a jar file ever Windows initialize and after the user logon (Not as a Service) that call a process with a GUI

I have a jar file and I wanted to run this file as a service. When this .jar executes, it opens another program that I want to be able to see it GUI. But note: only the second program needs a GUI (the .jar not). I open this second program using the following:
ProcessBuilder processBuilder = new ProcessBuilder(cmdCommand);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
Searching about this issue I saw that if I performed my .jar file as a service, I could not see the process started GUI because services executing in another desktop since Windows Vista. To create a service, I used Java Service Wrapper. On this tutorial, they teach how to use the wrapper.ntservice.interactive configuration to make for the service to display a GUI. They alert too:
"Starting with Windows Vista and Server 2008, Microsoft changed the way services are allowed to interact with the desktop. They decided that for security reasons, services are now only allowed to show their GUI on a special desktop that only the service can interact with. Unfortunately, this makes interactive features pretty much useless. We are looking into ways to display a GUI in a different way. Stay tuned.
If you do a search on the Internet with keywords of "Interactive services dialog Vista", you will find a number of references to the way Windows will display a dialog on the user desktop whenever a service has updated into GUI on the services desktop.
There are some important security reasons why this was disabled in Windows. But if you really need to reenable this functionality, it is possible to do so by editing the Windows Registry and setting HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\NoInteractiveServices to 0. Always be very careful editing the registry as you can easily mess up your system by changing the wrong values."
I've been tried to configuration the NoInteractiveServices to 0, but yet do not work.
So, how can I execute a .jar program when windows initialize and the user do the login, and still be able to see the GUI of my opened process?

Run Jar file on startup?

I have a Java application that lives in the system tray that I compile to a executable jar file. I would like to add the option within my program to add to the system startup items.
As I do not know of any uniform way to do this for all operating systems I assumed I would have to write code to do it for each one I intend to support so I started with Windows.
When I attempted to add it to the registry at [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] using the code available here I discovered that under Windows 7 and 8 unless I have administrator privileges (by running from an elevated command prompt) my edits to the registry do not apply.
Then I spent a day trying to figure out how to get the Jar to relaunch itself with admin privileges before I gave up on that hacky workaround.
Can the task I'm trying to achieve even be accomplished and if so how?
For the most part, you're actually looking to add the feature of auto starting on user login, rather than on system startup. For windows, if you add the registry entry under:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
This location does not suffer from permissions issues when run as an ordinary user, and has been supported for a long time under Windows (I'm thinking Windows 95 time frame here), so should be a safe change across all systems.
For Linux, assuming that the operating system is following the Open Desktop AutoStart specification, then you need to create the appropriate .desktop file in $HOME/.config/autostart/ and it should autostart on login in that case.
For Mac OS X, you need to create a launch agent plist in $HOME/Library/LaunchAgents. The Daemons and services documentation details how to construct this file.
Working around user privileges is not a good idea. The registry entry is the preferred way to go. Also keep in mind that the jar by itself is not executable, it requires the jvm, so what you might want to do is use a wrapper and register that.

Cannot run two instances of java webstart

I am unable to run more than one instance of java webstart at any given time.
For example, I am unable to run both the production & QA instance of an application at once, both of which are launched via java webstart. Additionally, I am unable to run the java webstart cache viewer at the same time as either the production or QA instance of my application.
I am however able to run any of the above three webstart launches when they are run in isolation of each other. When I try to bring up a second option, I see the 'Java Loading...' screen which then disappears and nothing happens.
Additionally, I have tried to delete the webstart cache (via the java webstart cache viewer) and I receive the following error regardless of which JRE I point to:
"Bad installation. Error invoking Java VM (execv)
'path to my javaw.exe'"
I expect both the problems I mention above are interlinked. I do not believe I have changed any configuration recently and I have been happily running java webstart for years.
Has anyone seen such a problem before?
Thanks,
Jack
EDIT: When the second instance of webstart attempts to run, during the display of the 'Java Loading...' screen I can see in the task manager that a new javaw.exe process is spawned. This process almost immediately dies though. I'm not sure how to inspect the failure in that process, but I expect it is similar to the failure when trying to clear my cache through the webstart cache viewer.
You may be able to use javaws from the command line to run a second instance in -offline mode. The verbose option is handy, too.
javaws -offline -verbose MyApplication.jnlp
I think it is because both instances of the application use the same folder as current working directory. I do not remember exactly but it is somewhere under user home and the folder contains the application name or something...
So, if this is correct the solution is to change the application name like "My Application - QA" vs. "My Application" used on production.
The name is somewhere in jnlp.xml.
The reason may be the startup parameters for client java/javaw, which do not allow to run more than one instance of Java. For example because of set debug port. These parameters can be set in the command line or in the Java Control Panel -> Java -> button View.

Any way to have Java Web Start automatically install shortcuts?

We're deploying a Web Start app that I and the other developers know how to install the shortcuts for (Control Panel -> Java on Windows, /Applications/Utilities/Java Preferences on OS X), but we have no reason to believe that end users know how to do this (or even know that it can be done).
Is there a way to either install the shortcuts automatically on first run, or (much preferably) ask the user if they'd like to install the shortcuts? I've been looking all over but the Sun/Oracle docs all say to go through the Java cache viewer, which I find completely ridiculous to ask of end users.
The JNLP specification allows for specifying that short cuts are to be created when the JNLP file is run.
Check the <jnlp><information><shortcut> tag.
The javaws command states:
Usage: javaws [control-options]
control-options include:
....
-import [import-options] <jnlp-file> import the application to the cache
import-options include:
....
-shortcut install shortcuts as if user allowed prompt
so you could probably do it through an invocation of javaws.
As of 1.6.0_18+, JWS offers the IntegrationService.
The IS can not only install/uninstall desktop shortcuts and menu items, but will report the success or failure of attempts to create/remove them.
Some minor points on standard use of shortcuts (through the JNLP file) using Windows and Linux. JWS will prompt the user only if the app. is sand-boxed. If the app. is trusted, it will go ahead and attempt to create the shortcut.
On Ubuntu Linux the menu items will fail silently, but the desk-top shortcut works. I have been meaning to investigate whether Ubuntu menu items will work better using the IS, by targeting the sub-menu to one of the standard sub-menus offered. If that is the case, menu items might be installed by the application targeting different menu structures on different OS'.

UAC and Java

Is it possible to ask for elevated permissions from within a Java Application? Suggestions I've seen seem to all be centered around running an external executable or setting up a manifest to request privileges on launch. These aren't available to, for instance, applets. Is there any way to request elevation from a running application?
UAC is not something that a running process can request (doesn't matter what language you are running in). You have to request elevation at launch time.
The way that most windows apps handle this (and make it look like they are requesting elevation) is to spawn an extra copy of themselves, requesting elevation, and passing sufficient command line arguments to the new process so that the appropriate dialog can be displayed.
This is why UAC elevation in a dialog is always initiated by a button click that opens a new dialog.
So, in the Java world, you just have to do exactly what everyone else has to do: launch your app again, requesting elevation. There are several ways to launch elevated, the 'run as' verb probably being the easiest.
Looks like Sun will have to handle that kind of situation in the JRE since there's no other way of doing elevated actions than by running an external process.
If JRE supported it, JVM would probably have to run a separate, elevated process for the java code requesting the elevation.
For now however, only the manifest or running an external application are the only solutions available as far as I know.
The question is, what do you need elevation for?
You have to use an external (native) application to do this. This post provides source code and a great explanation:
UAC Prompt From Java
You could use jna and do a ShellExec. For lpOperation use "runas" (this is not documented). Since you likely need the full path to the (current) JavaVM which is stored in the registrylook at registry access, part of JNA.
Goto the folder where java is installed.
open the properties of javaw.exe / java.exe and set "run as administator" option true.

Categories

Resources