Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error while using Timer ....

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii

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();
timer.schedule(obj,date.getTime(),1000);
}
}

class ReportGenerator extends TimerTask {

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

}

=======================================================================


IT GOT COMPILLED SUCCESSFULY

BUT FOLLOWING ERROR IS COMMING

================================


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 java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown 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)

C:\amit\amit\org>


===================================


Y THIS ERROR IS COMMING ???

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

later i will insert this code in servlet...




thanx and regards,
amit
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

pascal
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx

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,
amit
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


pascal
 
Jef Cumps
cowbird
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

regards,
amit
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go back and take a look at your classpath.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oK THANX
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 servlet...in which i have writen
//sendRedirect() function... now how to call the servlet using
// timer.sheduler();
timer.shedule(obj.date.getTime(),1000);

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

so
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic