Run Jar file on PC at startup
Try the following:
Write a batch file as follow ans save it as *.bat or *.cmd:
start javaw -Xmx200m -jar C:\Path\to\jarfile\TheJar.jar
Save the file created in the startup folder for all users, which should be C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
It is important to pay attention on the folder, because if your used Windows + R and shell:startup
shortcut like I usually do, it will only work for the current logged user.
Load desktop application on Windows startup
You can set up the exe as a windows service with these steps:
open command prompt as administrator
type this:
sc.exe create
<service_name>
binPath= "<path to your exe>
--service" DisplayName= "<somename>
" start= "auto"
Application start on logon with Java
Run At Login (manually)
Once you packaged your app, you can just put java -jar your-app.jar
in a .bat
file in the Windows startup menu.
If you don't want to create a jar
(e.g. because your app is simple enough) and always assuming you have your Java environment variables configured (JAVA_HOME
, PATH
....), you can just run directly your bytecode:
startup.bat
:
cd your/app/bytecode/path
java yourapp
See here how to start an app at startup.
Run at Startup (manually)
If you need the application to run before the user login you can use the Windows Task Scheduler, and use the trigger at startup, wrapping your application as described in this OS answer:
@echo off
REM Eventually change directory to the program directory
cd C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\
REM run the program
"C:\Program Files\Java\jdk1.7.0\bin\java.exe" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
Run at Startup (installer)
In case you really don't want any user interaction the only easy (and IMHO ethic) solution I imagine is the Installer.
You basically need a wizard installing your application for you (and for the user) configuring it to run at startup. There are many wizards, you can build your own or start your research having a look here.
Run at Startup (standalone)
Use the approach described here, writing something like this directly in your small application (original post here):
static final String REG_ADD_CMD = "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ";
private void exec(String[] args) throws Exception
{
if (args.length != 2)
throw new IllegalArgumentException("\n\nUsage: java SetEnv {key} {value}\n\n");
String key = args[0];
String value = args[1];
String cmdLine = MessageFormat.format(REG_ADD_CMD, new Object[] { key, value });
Runtime.getRuntime().exec(cmdLine);
}
Key
is your app name (not relevant), Value
is your jar file or the .bat command that runs your application (see above methods on how to package a jar or run a .class inside a .bat file), e.g. "C:\Users\YourApp.jar"
NOTE
As stated by the author, this approach should work with any versions of Windows, since they all use the same Startup\Run
registry entry. Though, keep in mind that this is not portable and you have probably to tune the script according to the MS OS version.
NOTE 2
Think twice before modifying the registry code directly from your application (the hacky way). For doing that you probably need Supervisor/Admin priviledges every time the application runs. Moreover doing that without correct, explicit user prompt can be seen as a malware/spam malicious practice.
Related Topics
Swingworker, Thread.Sleep(), or Javax.Swing.Timer? I Need to "Insert a Pause"
Using Enum Values as String Literals
How to Initialize Hashset Values by Construction
Spring: Why Do We Autowire the Interface and Not the Implemented Class
Spring Resttemplate Get with Parameters
Choose Between Executorservice's Submit and Executorservice's Execute
Get an Outputstream into a String
Best Way to Constrain User to Enter a Time in a Jtextfield
Gmail Login Using Selenium Webdriver in Java
How to Have a Textfield Inside a Label
How to Make Image Appear Randomly Every X Seconds in Java Using Timer
Graphics Rendering in Title Bar
How to Create a Sub Array from Another Array in Java
Drawing in Jlayeredpane Over Exising JPAnels