• Post Reply Bookmark Topic Watch Topic
  • New Topic

best way to run a jar file with an infinite loop  RSS feed

 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have this code where it has an infinite loop since I don't have any need for a display I just created it using a normal class and no UI, but the problem is that when I try to run the .jar file all I see is the mouse pointer turn to the windows loading circle indicating my program is loading but then after that it ends, although the code is executed as I used a verifier to know if it is executed and it did but then after that it stops, which is not what I want since I want it to keep on running forever. Then I tried adding some GUI to make it run forever but then since it has an infinite loop in it just works when it is executed but after that when I made some changes to check my verifier it didn't work anymore.

So to sum it up.

I have an infinite loop with a verifier, using a no GUI jar file it only executes once and closes, while using a GUI jar file it only executes my verifier once and that is when it is executed but after that it still keeps running but any changes I made to check if my verifier is still working will result to no response from my verifier.

P.S I tried to use a batch file to infinitely execute my no GUI jar file but I'm not sure if that's such a great idea.

Code for no GUI:




Wtih GUI


p.s you can remove the close function I just added it also I removed my verifier as it is an SMS service
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First impression is why bother using a UI for "infinite" jobs. So I would scratch the UI thing.

Since your program is doing SMS stuff (sending I assume), best is to write a log or to a database table whenever a SMS is sent or whatever.

The thing with SMS, the program should check if there indeed has SMS to send before the program is triggered. Simply put you really developing a producer/consumer program.

So maybe there is some queue for you to check? Given a queue, it may not have items in it, so you also need a Timer to check it every X seconds or something. The actual SMS stuff is most likely a TimerTask, similar to Runnable.

An example can be found here and here
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:First impression is why bother using a UI for "infinite" jobs. So I would scratch the UI thing.

Since your program is doing SMS stuff (sending I assume), best is to write a log or to a database table whenever a SMS is sent or whatever.

The thing with SMS, the program should check if there indeed has SMS to send before the program is triggered. Simply put you really developing a producer/consumer program.

So maybe there is some queue for you to check? Given a queue, it may not have items in it, so you also need a Timer to check it every X seconds or something. The actual SMS stuff is most likely a TimerTask, similar to Runnable.

An example can be found here and here


I do have a log of the sent SMS, also I might add your timer approach suggestion to this, but regarding my concern with the jar file only running once? will this still be an issue?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rodolfo tuble wrote:... but regarding my concern with the jar file only running once? will this still be an issue?


There is nothing related to jar files that should directly affect the ability to call loops. IMHO, this is an issue that needs debugging. Start logging your loop (or you can just use println()), to see if it is actually exiting the loop or stuck doing something in the loop. And of course, to see why it is happening.

I don't think you can avoid debugging the issue here.

Henry
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rodolfo tuble wrote:
I do have a log of the sent SMS, also I might add your timer approach suggestion to this, but regarding my concern with the jar file only running once? will this still be an issue?


Well depends. Is your program using only one database connection? What happens if the connection times out, the program crashes and end.

If database is needed, the connection should be closed at the end of every send task.



If you program correctly, the program will run forever. Under the above setup, if there is DB problem, only that task failed and shouldn't fail the entire program.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:
rodolfo tuble wrote:
I do have a log of the sent SMS, also I might add your timer approach suggestion to this, but regarding my concern with the jar file only running once? will this still be an issue?


Well depends. Is your program using only one database connection? What happens if the connection times out, the program crashes and end.

If database is needed, the connection should be closed at the end of every send task.



If you program correctly, the program will run forever. Under the above setup, if there is DB problem, only that task failed and shouldn't fail the entire program.


I always close the DB after, I have a final clause after the try catch. also how would I know the program is still running when there is no GUI for me to see when I execute the jar file?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rodolfo tuble wrote: also how would I know the program is still running when there is no GUI for me to see when I execute the jar file?


You don't. However, you may want to make use of the log writing something like the last timestamp if the wait time is long. So you can just check the last few lines of the log

Depending on what OS you use, you can check the process using ps command on Linux or task manager on Windows.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:

You don't. However, you may want to make use of the log writing something like the last timestamp if the wait time is long. So you can just check the last few lines of the log

Depending on what OS you use, you can check the process using ps command on Linux or task manager on Windows.


found it running on task manager under javaw.exe, I wonder if it will affect the heap memory just like when you run it in netbeans for so long and it will run out of heap memory? is it applicable to jar files as well? btw thank you for your guidance
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you have a bug (or haven't sized your app correctly) it shouldn't run out of heap.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rodolfo tuble wrote:I wonder if it will affect the heap memory just like when you run it in netbeans for so long and it will run out of heap memory? is it applicable to jar files as well?


It doesn't matter what environment you run your code in, the code will run the same. As for running out of heap memory, if that happens then your application will crash. IDEs like Netbeans will tell you that it crashed, and if your environment has a console then you'll see the stack trace. (Note: the "java" command has a console but the "javaw" command doesn't. Apparently you used "javaw".)

Your GUI version could possibly be made to work if you modified it to deal with concurrency properly. But if you just want the GUI for logging, it's a lot easier to take the advice already given and just put proper logging into your code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!