File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes error while using Timer ....  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "error while using Timer ....  " Watch "error while using Timer ....  " New topic

error while using Timer ....

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 812

i have wrote the following code --


package org;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

* @author user1
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
class MainApplication {

public static void main(String[] args) {
Timer timer = new Timer();
Calendar date = Calendar.getInstance();
// date.set( Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
date.set(Calendar.HOUR, 17);
date.set(Calendar.MINUTE, 12);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
// Schedule to run every Sunday in midnight
ReportGenerator obj = new ReportGenerator();

class ReportGenerator extends TimerTask {

public void run() {
System.out.println("Generating report");
//TODO generate report






C:\amit\amit\org>java MainApplication
Exception in thread "main" java.lang.NoClassDefFoundError: MainApplication (wron
g name: org/MainApplication)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at Source)
at Source)
at$100(Unknown Source)
at$ Source)
at Method)
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)




can anybody guide me please..... how to succesfully run the progrom..

later i will insert this code in servlet...

thanx and regards,

Thanks and Regards, Amit Taneja
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
this is a classpath problem. The JRE can not find your main class.
The fully qualified name of your MainApplication class is org.MainApplication. So you need to execute the command

C:\amit\amit\>java org.MainApplication
instead of
C:\amit\amit\org>java MainApplication

this should work.

there is also the -classpath (or -cp for short)flag you can pass to the JRE:
C:\projects\something>java -cp build org.something.App
(like this you specify that your org.something.App class is located in the build directory)

i hope this works for you now.

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 812

ya pascal it runs

but nothing is happening in output....
it must print the result "Generating Report" after every one second from the time specified ..
but no out put is comming

the code is the same as above..i posted...
kindly try this....
and run this..... ( do me this favor )

i m developing the sheduler....

thanx and regards,
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
add this line

to see when the Task will be executed the first time...

just remove the date.setXXX() methods (or use a different Timer.schedule() method) and it will start right now.

Jef Cumps
Ranch Hand

Joined: Nov 14, 2001
Posts: 53

I tried your little prgram, and it works fine with me.. after I changed the hour set in your date member to an hour in the past. Try it first without all setters on your date, just pass the newInstance() to the timer. That will work. Then start experimenting with the setters on your date, watch out for AM and PM mistakes! (check the calendar-API)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 812
At home
this runtime error is comming

D:\java_prac>java org.MainApplication
Exception in thread "main" java.lang.NoClassDefFoundError: org/MainApplication

why this now :-(

pls help me to sort this out..

thanx for your earlier reply

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Go back and take a look at your classpath.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 812
every thing is working now...but now this i have to implement in servlet
but there is problem .....

i cannot create my servlet object to be passed in
sheduler obj= new sheduler();
// above i created the instance of which i have writen
//sendRedirect() function... now how to call the servlet using
// timer.sheduler();

Ok can any body tell me how to call a webpage from a class that is not servlet it possible ?? actually i m developing a servlet which will be invoked at particular time and redirect page to some else page...

should i use threads in servlets ? can we use that ? but how..
i mean .... we should pass the object as an argument to Thread class..
can that object be servelt object ???

pls reply
Consider Paul's rocket mass heater.
subject: error while using Timer ....
It's not a secret anymore!