Solved

Adding schedule task in Windows task schedular Programatically in Java

Posted on 2009-04-09
9
949 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

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