Programming & Development / March 27, 2025

How to Automatically Run and Stop a JAR File on Windows Startup and Shutdown

run jar on startup stop jar on shutdown java autostart windows jar startup shutdown hook jar wscript jar startup java background process task scheduler jar java shutdown script jar process kill

Running Java applications (.jar files) automatically during system startup and stopping them gracefully on shutdown can be useful for background services, desktop agents, or local servers. This article explains how to:

  • Launch a JAR file silently at system startup
  • Stop or kill the JAR when the user logs off or shuts down
  • Use VBScript and Windows Task Scheduler for automation

🟒 Part 1: Running the JAR on Startup

There are multiple ways to do this, but the most common are:

βœ… Method 1: Using VBScript

Create a .vbs file like this:

vbscript

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "java -jar C:\Path\To\YourApp.jar", 0, False
0 – runs hidden
False – script continues without waiting

Save it as launchApp.vbs and place it in:

css

%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup

This will execute the JAR every time the user logs in.

βœ… Method 2: Using Task Scheduler (Recommended)

  1. Open Task Scheduler
  2. Create a new task
  3. Set Trigger: "At log on"
  4. Set Action:
  • Program: java
  • Arguments: -jar "C:\Path\To\YourApp.jar"
  1. Check "Run whether user is logged in or not"
  2. Optionally check "Hidden"

This gives more flexibility, especially if you're running services.

πŸ”΄ Part 2: Stopping the JAR on Shutdown

Java apps launched in the background (especially via VBScript) may persist beyond normal control. Here’s how to stop them:

βœ… Option 1: Java Shutdown Hook (Best for Clean Exit)

Modify your Java code to detect shutdown and handle cleanup:

java

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    System.out.println("App is shutting down...");
    // Close resources, save state, etc.
}));

βœ… Option 2: Use taskkill in a Shutdown Script

Create a batch file like this:

bat

@echo off
taskkill /F /IM java.exe /T

Save as stopApp.bat and use Group Policy or Task Scheduler to run this on shutdown:

  • Trigger: "On shutdown"
  • Action: run stopApp.bat

Or use a more targeted version:

bat

wmic process where "CommandLine like '%%test.jar%%'" call terminate

This only kills the specific JAR instance.

πŸ’‘ Bonus Tips

  • Logging: redirect output to a log file with > log.txt 2>&1
  • Silent JAR: use SystemTray, TrayIcon, or keep console hidden
  • Cross-platform? Look into Java services like Apache Commons Daemon or Java Service Wrapper

βœ… Summary

TaskMethodRun JAR on startupVBScript in Startup folder or Task SchedulerStop JAR on shutdownShutdown hook in code or Task Scheduler to killBackground & hidden runWshShell.Run with flags or Task Scheduler


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex