Solved

Adding schedule task in Windows task schedular Programatically in Java

Posted on 2009-04-09
9
942 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
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

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.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

839 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