aspose file tools*
The moose likes Java in General and the fly likes how to run java code in background Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to run java code in background" Watch "how to run java code in background" New topic
Author

how to run java code in background

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Rafael Prado Oliveira
Greenhorn

Joined: Apr 30, 2013
Posts: 28
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

Joined: Aug 20, 2011
Posts: 995
    
    2
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.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2321
    
  28

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to run java code in background