30

I have jar file which I want to run from windows scheduler in windows 7

I have Given the path of java.exe in path/script and -jar c:\abc\ab.jar in the arguments field.

But it does not run. I have also tried it with help of a bat file, but it does not run. What is needed to be done to run the jar file?

My jar file creates files and updates a database.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Nitesh
  • 485
  • 1
  • 6
  • 12

4 Answers4

48

Run a java jar from Task Scheduler (Windows 7 Professional 64 bit)

I'll be configuring it to run every 5 minutes.

Step 1, install java, make sure it is available from commandline

You should be able to run your java jar from the bare cmd commandline. Specify the full java directory like this: (This is the command I used)

C:\ProgramData\Oracle\Java\javapath\java.exe -jar C:\repo\curium.jar

Step 2, Open task scheduler:

In the Start menu search bar, search for "scheduler" and "Task Scheduler" should pop up. It is buried in the control panel: Control Panel -> System And Security -> Administrative tools -> Task Scheduler.

Step 3, make a new scheduled task:

In the left pane, right click "Task Scheduler Library". Choose: "New Basic Task". Give it any name.

Choose next. Click "Daily", (the configuration for re-runs every minute will be done later).

Choose next. Recur every 1 day. Have it start a 5 minutes from now. choose next. Click "Start a program".

Step 4, configure your java to run:

In the "Program/script" box put the full path to your java, your path may be different. Double check with cmd command where java

C:\ProgramData\Oracle\Java\javapath\java.exe

In the "Add arguments" box put this:

-jar C:\repo\curium.jar

Click next, click finish.

Step 5, Right click your new task for further configuration:

Right click your item -> Properties.

Choose the bubble: "Run whether user is logged in or not"

Under the "triggers" tab. Select your Daily task and choose Edit.

Step 6, configure the repeat-task triggers

Begin the task: "On a schedule".

Choose Repeat task every: "5 minutes". Choose the enabled checkbox. Click ok. Save.

You will be prompted for your windows username and password. Add and press OK.

Step 7, ensure it is working

Wait 5 minutes for it to run. Or force run it by right clicking -> Run.

Right click your task, choose properties. Choose "History" tab.

There should be a row there revealing when it ran. If you want to know if it completed successfully, you'll have to pipe your output to a log file.

Step 8, see if it survives a reboot

Reboot the computer, and see if it stays on. If it does you are done.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • Nice. you repeat task every 5 mins and I think you put "spring.jpa.hibernate.ddl-auto=none" so there is no change on database on every run. But what happens when some record update make app crashes and stops. so in every scheduled run it is crash and stops too. Is there any way to handle this? – Mahdi Aug 13 '17 at 09:23
  • 1
    Thanks Eric! It works just fine! Just to complement your answer, if anyone for any reasons wants to forward the sysout and syserr to a log file, you cannot do it on the Task Scheduler arguments, so just create a simple batch file like this: `@echo off java -jar abc.jar >> ..\logs\abc.log 2>&1` And call it from the the Task Scheduler same way: C:\abc\abc.bat – jfajunior Feb 08 '18 at 13:28
25

If your JAR creates files, I think you just to specify a working directory in the Start in field when you create your task. For example:

  • Program script: C:\Program Files\Java\jre7\bin\java.exe
  • Add arguments: -jar c:\abc\ab.jar
  • Start in: c:\abc
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 3
    Program script: C:\Program Files\Java\jre7\bin\java.exe should be in double quotes (" ") like "C:\Program Files\Java\jre7\bin\java.exe" because the folder name program Files contain space in between their name. – mystertyboy Jul 14 '16 at 18:59
  • The right solution for my issue! Thanks! BTW, do not forget about the "" – Cavaleiro Apr 03 '20 at 18:20
1

Step 0 : Setup

Add app.schedule.externally_managed=true in application.properties

Step 1: Create a New Task

Click Create

Provide details

Step 2: Trigger Details

Step 3: Action Details

Step 4: Actions

Step 5: View Task Details

Application invoked and shut down

Refer this and this for more details

craftsmannadeem
  • 2,665
  • 26
  • 22
0

Create Jar file or bat file

Creating Jar file > Project Right click > Export > Runnable Jar File > Destination > Run and Check

Now go to Task Scheduler and follow the below steps to execute the your jar/ bat file systematically way with Windows scheduler .

> 1> Create New Task

> 2> General add text

3> New Trigger

4> New Action

> 5> Add Conditions

> 6> Settings

> 7> Run the Task

> 8> Check History

Tarit Ray
  • 944
  • 12
  • 24