• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to run java code in background

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i always face a problem of system hanging and slow speed because of temporary file, i was thinking to write one java code which continuously runs in background and delete temp files continuously as they gets created.
can anyone tell me how do i run a java code in background which deletes all the files inside temp directory.

Thanks,
Punit
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Temporary files don't get created by programs just to make your system slow. They serve a purpose, whatever it is for the specific program that creates it. So I don't think it would be a good idea to write a program that deletes temporary files as soon as they are created, because other software will most likely not work properly anymore if you do that.

Besides that, writing a program that deletes files from a directory at specific times wouldn't be very hard.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Temporary files don't get created by programs just to make your system slow. They serve a purpose, whatever it is for the specific program that creates it. So I don't think it would be a good idea to write a program that deletes temporary files as soon as they are created, because other software will most likely not work properly anymore if you do that.

Besides that, writing a program that deletes files from a directory at specific times wouldn't be very hard.




yes, but how do i run that in background that is the question.
how do i do this: compile the program, run the program and exit from cmd, and it starts running in background.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:yes, but how do i run that in background that is the question.
how do i do this: compile the program, run the program and exit from cmd, and it starts running in background.



So you're going to do this even though it will probably cause other apps to not work correctly? Are you trying to play a nasty prank on somebody you don't like?

Compiling has nothing to do with it. You compile the way you always would. I don't know why you think that would be any different.

As for running in the background, that has nothing to do with Java. You run java in the background the same way you'd run anything else in the background. In Linux shells you put ab ampersand (&) after the command (and possibly prefix it with nohup). Windows may also use the ampersand, but I'm not sure. You can google for how to run in the background in whatever environment you're in.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threds...

Start a small program, that will be alive as long as you want, and every other hour it fires a new thread that will clean the temp files...
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Punit Jain wrote:yes, but how do i run that in background that is the question.
how do i do this: compile the program, run the program and exit from cmd, and it starts running in background.



So you're going to do this even though it will probably cause other apps to not work correctly? Are you trying to play a nasty prank on somebody you don't like?

Compiling has nothing to do with it. You compile the way you always would. I don't know why you think that would be any different.

As for running in the background, that has nothing to do with Java. You run java in the background the same way you'd run anything else in the background. In Linux shells you put ab ampersand (&) after the command (and possibly prefix it with nohup). Windows may also use the ampersand, but I'm not sure. You can google for how to run in the background in whatever environment you're in.



it may harm running application, that is why i am thinking to schedule this such a time when i am not using my computer (may be at night), so it will automatically clear temp files every night.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to write a program for this. You can write a shell script that deletes files in your temp folder. In windows you can write a batch file, and in linux you will write a bash script.

If you want to run it on a scheduled basis, you can schedule it using the tools provides by the OS. Windows has a task scheduler that can be configured to kick off your script daily at a certain time. Linux has cron that does the same thing. However, as said above, this is not safe because there might be programs running that need the temp files, and they might start crashing.

Instead of scheduling it, it's better to run this script everytime you start your machine. You can configure windows/linux to run scripts at login

I have to say though, you might be better off looking at why your temp folder is filling up so much that you need to clean it everyday. I have never cleaned my temp folder everyday, maybe like once a year or so.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic