Solved

Adding schedule task in Windows task schedular Programatically in Java

Posted on 2009-04-09
9
932 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 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

22 Experts available now in Live!

Get 1:1 Help Now