Run Jar file on startup? - java

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.

Related

Java on Linux - open file manager

I'm developing a JavaFX application for both Windows and Linux (Debian/Ubuntu distributions), currently on Linux Mint (I honestly don't have any experience with other distros than Mint). The application should be able to open folders with the system's file manager.
While on Windows I use this with no problems:
Desktop dt = Desktop.getDesktop();
dt.open(path);
This doesn't seem to work on Linux, so I thought of simply using Bash commands like:
ProcessBuilder builder = new ProcessBuilder("sh", "-c", "nemo " + path);
But this only works on Linux Mint Cinnamon, since KDE and other editions may come with different "default" file managers (like Dolphin or Nautilus).
Now is there a way, either programmatically with Java or with Bash, to get the associated file manager?
You need to find the default file manager.
You can use the xdg-mime command for that.
xdg-mime query default inode/directory return the default file manager.
Output if it's dolphin : dolphin.desktop
OR
xdg-open <directory> but it can start the wrong file manager sometimes.
See the documentation for more.
xdg-open <directory> has been deprecated
use 'gio open' instead. see source
you have to double think things... what is for you to open a folder ??? What does it mean? different operating systems (like windows, mac, or the like) have different file managers, so the mechanisms are subject to differences (mostly as they have evolved from previous environments, sharing nothing in common and without java in mind). Even there are operating systems with no file manager at all, as for example linux console applications don't assume a desktop is running at all and cannot call the file manager to open a window (where? no windows at all, only the black console screen) and show the files contained there.
Windows applications user the file Explorer to show folder contents and the communication with the explorer follows a path that doesn't share anything in common (while the mechanism is similar, anyway) with the one used in linux.
In linux you have the added problem that there are several (better said, many) desktop environments competing and doing things differently between each other.
The java support doesn't dig so deep in the desktop environment to allow for the task you face on in a desktop environment independent way.
Anyway, there are several file managers written in java that do the same and can be run in either environment (java, windows and the mac), so you can run other route, and use a common file manager for the task. And probably communicating with a program written in java is easier from your application.

Install and uninstall a java application in ms-windows

I have created a java application.
I also created another java application which install the first application.
The program runs perfectly.
However the program isn't displayed in control panel->programs.
I also want to create an uninstaller (let's say unintaller.bat). But i want to works like any other uninstaller. When someone goes to Control Panel->serach program->uninstall this program it will run the uninstaller.bat
In other words, the concept is how to declare the java application in windows...
Any idea???
Installing an application doesn't necessarily mean registering it with the operating system. For example, for a Mac, I can open an App without having it added to the Applications folder.
Same with Windows. Just because a new file can be added to the Start menu, it doesn't mean it was registered.
Use a Windows installer. There are tons that are open source, and even MS provides a free one: http://support.microsoft.com/kb/942288.
This will allow you to install an uninstall an application using standard tools. Yes. It can be called from a bat file.
Java open source installers: http://java-source.net/open-source/installer-generators

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

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();

How to install mysql dynamically from within java?

I am currently trying to create an installer for a application that i created. And i want the installer to be able to run on mac, windows and linux for now. Hence, i chose to make my installer in java.
Now, the problem that i am facing is i have to install mysql from within java without giving the user all those unnecessary choices during the setup phase because it really necessary for them to know it. I know this can be done because while installing wamp, this is how mysql is installed. I only need to bother the users for the right things.
So, can somebody tell me how it can be done?
Thanks in Advance
For Mac and Linux I would let your installer start a shell script. However on Macs it is not common to have "installers". You should consider to just deliever binaries which the user can copy via drag&drop with his mouse. For that you usually only make an immage file (*dmg) which gets mounted by the finder and opens in its own finder window. You can have a symbolic link of the Applications folder on that *.dmg and a background image with some text: "drag here --->"
This might not be exactly what you're after, but how dependant are you on using mysql as the DBMS? If you use fairly standard SQL and this suits your application, maybe you could switch to Apache Derby? That will allow you to include the whole DB as a part of the installation, and completely invisible to the user.
See: http://db.apache.org/derby/
(caveat: I don't remember the details of the Apache license, but I'm assuming there's no issue there)

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