Solved

Adding schedule task in Windows task schedular Programatically in Java

Posted on 2009-04-09
9
945 Views
Last Modified: 2012-06-27
Hi,

I am working in java.

I need to add .bat files in windows task schedular programatically using Java. Each batch file has its own time of executing. This will be decided by program logic. When to execute what .bat file.


Please guide

Kind Regards,
0
Comment
Question by:tia_kamakshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 500 total points
ID: 24112531
There's no direct way of doing this, as Windows task scheduling is platform dependent.
You could write some code in (say) VB, and call it from your Java application, but I wouldn't really advise that.
Would it be viable to create your own task schedular? With the recent version of Java, you can create Windows services; so you could code your own service to run any required tasks at specific times.

If so, I'd recommend using this wrapper:
http://wrapper.tanukisoftware.org/doc/english/download.jsp
0
 
LVL 92

Expert Comment

by:objects
ID: 24112875
why not have your java app schedule and run them instead using something like quartz

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24114809
Runtime.exec the Windows 'at' command with admin privileges
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:tia_kamakshi
ID: 24116652
Hi,

Thanks for your reply.

Looks possible if we see the URL
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0922.mspx

But I don't know how to do in Java

Using Runtime.exec, I can run the .bat file at the time my program is running but I cannot schedule my .bat file...

I am not sure if it possible in Java.. I am happy to buy any third part tool for this, which connects with my Java application as well

Please suggest
0
 
LVL 92

Expert Comment

by:objects
ID: 24120405
just use java to do the scheduling instead of Windows task scheduler.
0
 

Author Comment

by:tia_kamakshi
ID: 24123121
Many Thanks for your help.

Looks I need to use
http://wrapper.tanukisoftware.org/doc/english/download.jsp

I have read the documentation of it. I have created very simple java file, to see how it works with it

But I am getting java.lang.NoClassDefFoundError: TimerTest

As written in the documentation, I have copied and renamed batch files in my folder "app"
 -> InstallMyApp-NT.bat
-> MyApp.bat
-> UninstallMyApp-NT.bat
-> wrapper-windows-x86-32.exe

And one folder up of app folder I have copied conf folder with file "wrapper.conf"

I have attached "wrapper.conf" file with name wrapperconf.txt

When I am running f.bat file "MyApp.bat". Also I have added path to my "app" folder to my classpath of enviroment variable

I am getting error

STATUS | wrapper  | 2009/04/11 19:55:32 | --> Wrapper Started as Console
STATUS | wrapper  | 2009/04/11 19:55:32 | Java Service Wrapper Community Edition 3.3.3
STATUS | wrapper  | 2009/04/11 19:55:32 |   Copyright (C) 1999-2009 Tanuki Software, Ltd.  All Rights Reserved.
STATUS | wrapper  | 2009/04/11 19:55:32 |     http://wrapper.tanukisoftware.org
STATUS | wrapper  | 2009/04/11 19:55:32 |
STATUS | wrapper  | 2009/04/11 19:55:32 | Launching a JVM...
INFO   | jvm 1    | 2009/04/11 19:55:32 | java.lang.NoClassDefFoundError: TimerTest
INFO   | jvm 1    | 2009/04/11 19:55:32 | Caused by: java.lang.ClassNotFoundException: TimerTest
INFO   | jvm 1    | 2009/04/11 19:55:32 |       at java.net.URLClassLoader$1.run(Unknown Source)
INFO   | jvm 1    | 2009/04/11 19:55:32 |       at java.security.AccessController.doPrivileged(Native Method)
INFO   | jvm 1    | 2009/04/11 19:55:32 |       at java.net.URLClassLoader.findClass(Unknown Source)
INFO   | jvm 1    | 2009/04/11 19:55:32 |       at java.lang.ClassLoader.loadClass(Unknown Source)
INFO   | jvm 1    | 2009/04/11 19:55:32 |       at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
INFO   | jvm 1    | 2009/04/11 19:55:32 |       at java.lang.ClassLoader.loadClass(Unknown Source)
INFO   | jvm 1    | 2009/04/11 19:55:32 |       at java.lang.ClassLoader.loadClassInternal(Unknown Source)
INFO   | jvm 1    | 2009/04/11 19:55:32 | Could not find the main class: TimerTest.  Program will exit.
INFO   | jvm 1    | 2009/04/11 19:55:32 | Exception in thread "main"
ERROR  | wrapper  | 2009/04/11 19:55:32 | JVM exited while loading the application.

Please help me in fixing it

Many Thanks again for your great help

import java.util.Timer;
import java.util.TimerTask;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
 
 
public class TimerTest {
 
	public static
	void main(String[] args)
	throws Exception
	{
		int initialDelay = 5000;
	    int interval = 1000;
 
		Timer timer = new Timer();
		timer.schedule(new TimerTask()
		{
		    public void run()
		    {
		        System.out.println("I am running");
		        try
		        {
		    	    TimerTest.createFile();
				}catch(Exception ex)
				{
				}
		    }
 
		}, initialDelay, interval);
	}
 
 
	public static void createFile()
	            throws Exception
	    {
			String strFilename = "C:\\java\\schedular\\hello.txt";
			Calendar cal = Calendar.getInstance();
		    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-Hmmss");
 
		    Random generator = new Random();
		    String DateIs = sdf.format(cal.getTime());
 
        String strUniqueReference = String.valueOf(generator.nextInt())+DateIs;
 
 
	        BufferedWriter bufferedWriter = null;
 
	        bufferedWriter = new BufferedWriter(new FileWriter(strFilename));
 
	        bufferedWriter.write(strUniqueReference);
 
	        if (bufferedWriter != null)
	        {
	            bufferedWriter.flush();
	            bufferedWriter.close();
	        }
    }
}

Open in new window

wrapperconf.txt
0
 
LVL 92

Expert Comment

by:objects
ID: 24123732
try specify the (abosolute) full path to jar in your classpath

0
 

Author Comment

by:tia_kamakshi
ID: 24136361
Many Thanks,

http://wrapper.tanukisoftware.org/doc/english/download.jsp works.

We need to add classpath in its configuration file like
http://www.drftpd.org/forums/viewtopic.php?t=1782

or

wrapper.java.classpath.2=

Now it is giving me problem in installing as windows service.

Anyway, my this question is resolved.I will put my new problem with "tanukisoftware" in new question.

Many Many Thanks to all for helping me great.
0
 

Author Closing Comment

by:tia_kamakshi
ID: 31568778
Many Thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question