Run java program at windows startup

Code for Auto starting a java application on windows startup [duplicate]

I have a Java desktop application using netbeans 6.5.1. I was wondering if anybody can help me in suggesting code for autosatrting the Jar file on windows start up.

Thanks in advance Bhavi

2 Answers 2

It depends on what you mean by «Windows Startup». Running it in the background without a user logging in, you’d have to create a Windows Service wrapper.

In case «Windows Startup» means «run when the user logs in», create a batch file that starts your application and put a link to that batch file into the user’s Autostart folder (or the «All Users» Autostart folder).

EDIT To clarify what I mean according to your comments.

You said you were a .NET programmer, so we can just talk like you’d develop a .NET application:

If your program should behave like a Windows Service, you’d need some kind of service wrapper that acts as the «real» Windows service and starts your program when it is started using the Service Manager (you can not develop a Windows Service in Java so far, so you need this wrapper).

If your program should behave like a normal application and just be started whenever a user logs on (example: Skype or WinAmp Agent), you’ll just have to start your program putting a link to it into the user’s AutoStart folder.

In case of 1), you’d have to follow the answers of the other posters — I’ve never done that myself before, but I’m sure it can be done. (Maybe this link will help you: http://edn.embarcadero.com/article/32068)

In case of 1), just create a batch file that runs your application and create a link to that batch file in the user’s/All Users’ AutoRun folder.

How to create a windows service from java app

I’ve just inherited a java application that needs to be installed as a service on XP and vista. It’s been about 8 years since I’ve used windows in any form and I’ve never had to create a service, let alone from something like a java app (I’ve got a jar for the app and a single dependency jar — log4j). What is the magic necessary to make this run as a service? I’ve got the source, so code modifications, though preferably avoided, are possible.

19 Answers 19

Apache Commons Daemon is a good alternative. It has Procrun for windows services, and Jsvc for unix daemons. It uses less restrictive Apache license, and Apache Tomcat uses it as a part of itself to run on Windows and Linux! To get it work is a bit tricky, but there is an exhaustive article with working example.

Besides that, you may look at the bin\service.bat in Apache Tomcat to get an idea how to setup the service. In Tomcat they rename the Procrun binaries (prunsrv.exe -> tomcat6.exe, prunmgr.exe -> tomcat6w.exe).

Читайте также:  Звуки waw для windows

Something I struggled with using Procrun, your start and stop methods must accept the parameters (String[] argv). For example «start(String[] argv)» and «stop(String[] argv)» would work, but «start()» and «stop()» would cause errors. If you can’t modify those calls, consider making a bootstrapper class that can massage those calls to fit your needs.

With Apache Commons Daemon you can now have a custom executable name and icon! You can also get a custom Windows tray monitor with your own name and icon!

I now have my service running with my own name and icon (prunsrv.exe), and the system tray monitor (prunmgr.exe) also has my own custom name and icon!

Download the Apache Commons Daemon binaries (you will need prunsrv.exe and prunmgr.exe).

Rename them to be MyServiceName.exe and MyServiceNamew.exe respectively.

Download WinRun4J and use the RCEDIT.exe program that comes with it to modify the Apache executable to embed your own custom icon like this:

Now install your Windows service like this (see documentation for more details and options):

Now you have a Windows service of your Jar that will run with your own icon and name! You can also launch the monitor file and it will run in the system tray with your own icon and name.

One more option is WinRun4J. This is a configurable java launcher that doubles as a windows service host (both 32 and 64 bit versions). It is open source and there are no restrictions on its use.

(full disclosure: I work on this project).

Yet another answer is Yet Another Java Service Wrapper, this seems like a good alternative to Java Service Wrapper as has better licensing. It is also intended to be easy to move from JSW to YAJSW. Certainly for me, brand new to windows servers and trying to get a Java app running as a service, it was very easy to use.

Some others I found, but didn’t end up using:

  • Java Service Launcher I didn’t use this because it looked more complicated to get working than YAJSW. I don’t think this is a wrapper.
  • JSmooth Creating Window’s services isn’t its primary goal, but can be done. I didn’t use this because there’s been no activity since 2007.

If you use Gradle Build Tool you can try my windows-service-plugin, which facilitates using of Apache Commons Daemon Procrun.

To create a java windows service application with the plugin you need to go through several simple steps.

Create a main service class with the appropriate method.

Include the plugin into your build.gradle file.

The same script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:

Configure the plugin.

Run createWindowsService gradle task to create a windows service distribution.

That’s all you need to do to create a simple windows service. The plugin will automatically download Apache Commons Daemon Procrun binaries, extract this binaries to the service distribution directory and create batch files for installation/uninstallation of the service.

In $/windows-service directory you will find service executables, batch scripts for installation/uninstallation of the service and all runtime libraries. To install the service run

-install.bat and if you want to uninstall the service run

Читайте также:  Зависает окно при загрузке windows

-uninstall.bat . To start and stop the service use

Note that the method handling service start should create and start a separate thread to carry out the processing, and then return. The main method is called from different threads when you start and stop the service.

For more information, please read about the plugin and Apache Commons Daemon Procrun.

How to make a Program run on startup in Windows 10

There may be some programs that you may want to always run on startup or boot. Let us say that the first thing you always do is fire up your browser and start browsing the web. Sure, when your Windows PC boots to desktop, you can always fire up your browser manually and enter the URL, but if you wish to make it auto-start, then you can easily make Programs, like say a Browser, run on startup automatically every time in Windows 10/8/7.

Make a Program run on startup in Windows 10

First, you want to check in the Program’s settings and see if there is a setting that allows you to make it run at every startup. If there is then it answers the question easily. If not, then there are three other ways you can do it. Let us take a look at them:

1] Place Program shortcut in Startup folder

The simplest way would be to place a shortcut of the Program in the Windows Startup folder.

The Current Users Startup folder in Windows is located at:

These programs start up for the current logged in user only. To directly access this folder, open Run, type shell:startup and hit Enter.

The All Users Windows startup folder is located at:

To open this folder, bring up the Run box, type shell:common startup and hit Enter.

You can add shortcuts to the programs you want to start with your Windows in this folder.

2] Add Programs to startup using freeware

While the built-in MSCONFIG or System Configuration Utility allows you to disable or delete startup entries, it does not allow you to add startup programs. There are many free tools that let you manage startup programs.

To add startup programs easily, you can make use of freeware like Chameleon Startup Manager or Quick Startup. Both are feature-rich startup managers that let you easily add startup programs along with their launch parameters.

3] Make Program run with Windows boot via Registry

You can make use of the Registry Startup Paths to add programs to start with every boot. You can use the Run and RunOnce registry keys to make programs to run each time that a user logs on or run just once.

The registry keys are located here. They are for Current User and for All Users – Run every time or run just once:

You can read more about this on MSDN.

4] Use Task Scheduler to make programs run at startup

You can create a Basic Task Wizard using Windows Task Scheduler to make a program start at boot every time, by using the When the computer starts option as the Task Trigger.

PS: This post will show you how to open Windows Store apps on startup.

Читайте также:  Что ест батарею windows

Automatically run program on Windows Server startup

I am trying to install an app on a windows server that is always on, but I am running into problems. I have heard that I should not make it a Windows service (and would personally rather have a dialog app so I can see progress, etc) so I am trying to use the task scheduler. However, the task scheduler keeps trying to close my app prematurely, when it should only open it and leave it open.

Any ideas how I could have a dialog app run on startup (and stay running) on my server?

4 Answers 4

You should make a Windows Service; it’s the only reliable way to do this.
Otherwise, you will run into problems if, for example, the user logs off.

If you want UI, you can make a separate GUI that communicates with the service (probably using WCF).

You can do it with a Scheduled Task which wont have problems with users logging on/off.

  1. Open Task Scheduler, Windows Key + R
  1. Click Action menu > Create Task

  1. Change the User to a Administrator or preferably a Service Account:

  1. Set At Log On:

  1. Set a program to start:

It’s a very old question, but for those who coming here via Google:

Start the command prompt and type shell:Startup

This will open a window in the otherwise hidden AppData path for the Startmenu/Programs/Startup -> Add a shortcut to the executable for the program you want to start.

This will of course only apply to the logon account that you’re using at the time.

For all users type shell:Common Startup

I prefer to create a task in the Task Scheduler and set the trigger as «On Startup», but you can also use Startup Scripts.

How do I set a program to launch at startup

I have a small application with a CheckBox option that the user can set if they want the app to start with Windows.

My question is how do I actually set the app to run at startup.

ps: I’m using C# with .NET 2.0.

11 Answers 11

Several options, in order of preference:

  1. Add it to the current user’s Startup folder. This requires the least permissions for your app to run, and gives the user the most control and feedback of what’s going on. The down-side is it’s a little more difficult determining whether to show the checkbox already checked next time they view that screen in your program.
  2. Add it to the HKey_Current_User\Software\Microsoft\Windows\CurrentVersion\Run registry key. The only problem here is it requires write access to the registry, which isn’t always available.
  3. Create a Scheduled Task that triggers on User Login
  4. Add it to the HKey_Local_Machine\Software\Microsoft\Windows\CurrentVersion\Run registry key. The only problem here is it requires write access to the registry, which isn’t always available.
  5. Set it up as a windows service. Only do this if you really mean it, and you know for sure you want to run this program for all users on the computer.

This answer is older now. Since I wrote this, Windows 10 was released, which changes how the Start Menu folders work. including the Startup folder. It’s not yet clear to me how easy it is to just add or remove a file in that folder without also referencing the internal database Windows uses for these locations.

Оцените статью