I have a little .jar that executes a simple system administration task and so it needs to be run with elevated privilege. I've been researching this for hours and now know that it can be done in three ways:
1) ran from an elevated cmd prompt
2) convert the .jar to .exe and bundle it with a manifest file
3) use another .jar to launch my .jar and ask for permission.
Option 1) won't work for me because this will need to be deployed to other users that won't know how to do this. Option 2) isn't ideal because I chose to write this app in Java for its portability. This will likely be run on different systems and Java seems the most compatible. So that leaves Option 3) and is where my question comes in. I can't seem to sift through the multitude of info out there on how to accomplish creating a wrapper for my app. With my specs in mind what do you all recommend for creating a wrapper .jar that will prompt the user to allow my .jar to run? Thanks
On Windows, it is possible to run a Java application either as a desktop application, or as a Windows Service in the background. In the case of a Service, the Wrapper needs to be able to be installed, removed, started, stopped, have its status queried, etc. Depending on whether the application has a GUI or is meant to be run in a command window also determines how it will be run.
On Windows systems, the most way of launching the Wrapper is to make use of dedicated batch files to perform each action of controlling the Wrapper. This makes it possible for the end user to double click on the batch file icons or set up links in menus, just have a look if you have the java runtime env.
Nice tutorial: http://wrapper.tanukisoftware.com/doc/english/qna-service.html
Here it has some other possibilities, using Dedicated Batch File, Command Based Batch File or Standalone Binary : http://wrapper.tanukisoftware.com/doc/english/launch-win.html#dedicated
Think you can do this with .bat file. Make sure you have java runtime env, so that you can execute jar file using java -jar command.
If your looking to force the user to use elevated permissions then pure Java isn't going to cut it. I suggest you write native code and use the Java Native Interface (JNI)
Related
I read SO q&a about launch java as windows service wrappers, but can't find/choose product, suitable for my requirements :
Wrapper reads all java launch parameters from config file. In registry must be only commands to run wrapper itself - path to exe and maybe path to config. Always grant admin rights to change app parameters in registry is not comfortable.
Working dir and path to application also must be in config. I want to place all wrapper files deep into program, because user not intrested in it.
No dependencies(.net, python, etc). I don't want to extend requirements of my program.
Free for every windows OS, include win64.
Simple and lightweight. Size of my program with all 3rd party jars is 12Mbytes. I don't want to add 20Mbytes wrapper.
Automaticaly restart if app process dies.
Preferably single exe files for all windows platform.
I try and lookinkg throught different wrappers and tired a bit. Best suitable till now is:
jslwin (http://jslwin.sourceforge.net), but i don't know how mature it is(version 0.99). Also parameter lists in config is a bit complex(need to edit parameters quantity).
winrun4j (http://winrun4j.sourceforge.net), but i don't know how mature it is(version 0.45). Also it needs a special class for working as a service.
Also i try or look throught:
YAJWS (http://yajsw.sourceforge.net/) too big (30mb), can't make it work with custom files layout.
Advanced Installer (http://www.advancedinstaller.com) too big (50mb).
nssm (http://nssm.cc/) store launch parameters in registry(no config)
winsw (https://kenai.com/projects/winsw) .net required, stops after process dies
commons-daemon, procrun (http://commons.apache.org/proper/commons-daemon/index.html) store launch parameters in registry(no config), can't understand this product, i think it's complex.
jsmooth (http://jsmooth.sourceforge.net) complex and old(last update 2007)
javaservice (http://forge.ow2.org/projects/javaservice/) old (last update 2006)
runasservice (http://runasservice.sourceforge.net/) requires .net
windows rktools - sc, instsrv, srvany (http://support.microsoft.com/?scid=kb%3Ben-us%3B137890&x=16&y=7) - store launch parameters in registry(no config)
jwrapper (http://www.jwrapper.com/) seems not what i need. It packs java program in exe file with JRE. It's not about windows services.
firedaemon (http://www.firedaemon.com/) not free. And maybe it's not what i need.
launch4j (http://launch4j.sourceforge.net/) cool ability "search or download java". Maybe i add something like this to simplify installation, but it is still not about subj.
Java service wrapper (http://wrapper.tanukisoftware.com/) not free for win64.
Tell me please which wrappers suits my needs? Or maybe i think wrong about wrappers above and some of them suits my needs to?
I have had similar issues over the years. Its not that they all are inferior, its that they are all pretty hard to integrate nicely with your own project.
You may want to check out stork: https://github.com/fizzed/stork
It'll help create launchers for your Java console or daemon apps, and they will work across all major platforms. There is also a maven plugin in case you wanted to tie it into your existing build process.
I'm not sure if JWrapper will fulfil all the requirements like 1 and 2 in the way that you want it since rather than you dictating how it does the launch and where it puts things etc JWrapper instead handles that stuff in its own way but it does produce executables for win/mac/linux and more importantly it does support windows service installation as well as macos and linux daemon installation:
http://www.jwrapper.com/guide-installing-a-service.html
The service support though is for paid licenses, the free version doesn't currently support it.
Disclosure: I work for JWrapper
I have written a Java application that includes a self updater. The self updater loads new program versions from a web server and replaces the application files. While this works perfectly if the application is installed e.g. in the users home directory, it fails on windows machines if it's installed in the C:\Program Files folder. This is because the JVM is executed under the user’s account which has no write access to the program directory.
If a native program, e.g. an installer, tries to write to the program folder, usually a popup appears asking the user to permit the write operation. This doesn’t happen for java applications. Why?
Is there any way to achieve that a write operation of a Java program to a restricted folder brings up the security popup so that the user can permit access to that folder?
Thanks for your responses. According to the answers I see the following options:
Java Web Start
For me this is not an option for end users. I think that no one can expect from an ordinary end user to know what Java Web Start is, what it’s good for and how it’s used e.g. I doubt that an ordinary Windows user knows how to uninstall a Java Web Start application.
Use an exe-launcher with manifest to launch the Java application
As far as I understand this solution the exe-launcher would request extended execution right at application start. This is not exactly what I want, cause for my use case it would be sufficient to get extended rights if an update is available and not on every application start.
Perform the update operation by calling a native executable
One could call a native executable to let it perform the update operation. In this way the application would only request extended rights if an update is available. This sounds not bad but includes some native coding for Windows and doesn’t work on other platforms.
Install a launcher in program folder and the application in user home
One can place a launcher in the program folder that calls the application that is installed in the user’s home directory. In this way it would be possible to update the application in the user’s home folder.
I use InnoSetup for installing my application on Windows and as far as I can see it a split installation is hard to achieve with this installer and probably with other too.
Install the complete application in the user’s home directory
Because the user has write access to his home directory there is no problem at all. For me this looks like the best option cause of its simplicity.
If you are using inno setup compiler to generate your launcher, then you can change your app directory permission.
For example, if you need full control and want to update files under AppName/data folder
[Dirs]
Name: "{app}";
Name: "{app}\data"; Permissions: everyone-full
[Files]
Source: data\*; DestDir: {app}\data\; Flags: ignoreversion recursesubdirs createallsubdirs; Permissions: everyone-full
Unfortunately the increased permissions need to be requested when you first start the program, you cannot promote to them later. Programs that look like they do that are actually restarting themselves with the higher privs behind the scenes.
I had a problem like this a few years ago with a Java app and in the end I installed the application to the user data folder instead of program files as otherwise the auto-updating was a nightmare. You can still add it to the start menu so to a user it looks exactly like any other program.
I would like to "link" few file extensions to my java application under windows. When user double clicks file with "linked" extension, I would like to open my app and I need to know path to file that launched app.
If you deploy the app. using Java Web Start, an interest in file-types can be declared in the launch file. See the demo. of the file services, which..
..prompts the user to associate file extension .zzz (simply a file type unlikely to clash with existing file associations) of content type text/sleepytime. ..
When the user double clicks a .zzz file, it should open in the app. Actually, the word 'prompts' there is not the whole story. If you launch the sand-boxed version you will be prompted as to associating the file-type. The trusted version does not prompt.
To add more user-control to the process, look to the IntegrationService that was introduced in 1.6.0_18 (I don't have a demo. of that one yet). You might run it at start-up, after checking with the user.
this would have to be done during installation. how are you planning on letting your user install your application?
you have to realize at this stage that you just made things a whole lot more complicated. registering file extensions means meddling with the registry. what happens if the user doesn't want your application anymore? or moves the file that launches your application?
you'll have to pick an installation creator. here's a so question about that: https://stackoverflow.com/questions/3767/what-is-the-best-choice-for-building-windows-installers
and then you'll have to learn that installer creator's language. here's how what you're asking for is done in nsis. remember that the script takes care of questions like "if the user uninstalls my application, and i didn't change the file associations at install time, should i then remove these file associations on uninstall?" so it's a bit long. here it is anyways: http://nsis.sourceforge.net/File_Association
maybe it can be done in an easier way in another installer creator.
however, in this example, you give the register function of nsis the start command for your application, and then it adds %1 to it in the start command of the windows file association. so you should give it the start command
javaw -cp installpath\yourcode.jar package.name.MainClassName
and then things should work out. this will take some experimenting of course, and you will have to be quite sure about how to start your application from the command line.
good luck!
On his blog Scott Kovatch writes:
Without getting into too much detail, typing ‘java MyAWTCode’ from a Terminal window violates a whole lot of assumptions about what an application is on Mac OS X, and needs a lot of cooperation between the AWT and the Process Manager to sort it out.
http://skovatch.wordpress.com/2011/01/03/secret-smoke-screens/
Out of curiosity - what assumptions are violated? Surely this is just a candidate for an API call with callbacks?
Sure, I can elaborate on that a bit.
The Process Manager starts with the assumption that all applications that present a UI on Mac OS X are bundled, are packaged in a folder named Application.app, binary in Contents/MacOS/Application, and most importantly, have an Info.plist to get things like the name of the application that will be shown in the application menu. When you run a Java application from the command line (Swing or SWT) there is no Info.plist, so we had to create a CFDictionary to be passed off to a private SPI that would register the application, give it a proper name in the Dock -- as opposed to just 'java' -- and could be force-quit.
Even then, it's not perfect, because the Dock also assumes it can store an alias to the bundled application when you right-click and choose Keep In Dock, but since there isn't one that fails silently. There's no way to store a shortcut or command line to start the application like Windows can.
The SWT just calls TransformProcessType, which is a start but is nowhere near sufficient to turn a Java application into a full UI application. For doing pure SWT testing and development it's enough to get you going. When you create an Eclipse RCP-based application for deployment you end up with a bundled application with the Eclipse launcher, and plugins and features, and you're ready to go.
Of course, if you go the extra mile and package your Java application into a bundle, this is all moot, but the vast majority of developers coming from other platforms don't bother and just want to run an executable JAR file or even a folder of class files with a shell script.
I am not sure what he had in mind, but I guess a big difference is the file structure: a normal MacOS X application is a bundle with the structure NameOfTheApp.app/Contents/MacOS/NameOfTheApp , and specific files in the Contents directory. When we use the terminal with a "java" command, the JVM has to create a "virtual" application specific to the Java code, and handle all the MacOS events for it. Also, when you open an application twice in the Finder, it simply activates the application the second time, while you need to launch separate applications every time you use "java MyAWTCode".
I have written a very simple Java application. Can anyone tell me how to create a launcher like icon to run that application both in Ubuntu and Windows ??
Thanks in advance..!!
An executable JAR should work fine for a launcher on both Windows and Linux. However, that won't get you a custom icon.
On Windows, you can use JSmooth, which will create a .exe wrapper around your JAR file. The JSmooth program will let you create an icon for the EXE as well (it also has options such as getting the user to download the necessary version of the JVM, or only permitting one instance of the program to run).
If your java application is to be distributed from a web server, you should have a look at Java Web Start which can do what you ask for based on a JNLP-file. Notably see
http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/javaws/developersguide/faq.html#104
Ubuntu and Windows will both have different ways to launch the application. I see two routes to follow here. One is to use Java Web Start and use a web interface to start your application. The other is to search for some sort of 3rd party installer that will create setup/installation programs for all the platforms you want to use.
I actually used a third party install program before, InstallAnywhere, but it was almost ten years ago. It offered the functionality you are looking for, though.
There are different ways to do this, sometimes the simplest is the best approach. One solution as suggested is a simple starting script. Roll your application into a jar, then include a script that does nothing more than "java -jar myscript.jar". I do this all the time for internal customers that may be running various types of *nix and whatever version of windows (a few macs as well). How sophisticated you need it to be depends on the audience served.
Create a bat/shell script which starts your application.
And than create a shortcut/launcher for it.
Shortcut file in windows has extension lnk.
Update
See example - SQuirreL launch file.