Solved

Adding schedule task in Windows task schedular Programatically in Java

Posted on 2009-04-09
9
931 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
9 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 500 total points
Comment Utility
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
Comment Utility
why not have your java app schedule and run them instead using something like quartz

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Runtime.exec the Windows 'at' command with admin privileges
0
 

Author Comment

by:tia_kamakshi
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

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

Author Comment

by:tia_kamakshi
Comment Utility
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
Comment Utility
try specify the (abosolute) full path to jar in your classpath

0
 

Author Comment

by:tia_kamakshi
Comment Utility
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
Comment Utility
Many Thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now