Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stevejobbers

macrumors member
Original poster
Aug 19, 2008
80
0
I wrote a simple Java program that I'd like to run when I login and have it continue to run in the background (it's a server for a simple client/server program).

Currently I have a 'command' file that I'm using to start the process, but I have to keep doing it manually and it opens a Terminal window on the dock. I'm hoping there is a way to configure it to run in the background automatically when I log in.

Thanks guys!!!
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
I wrote a simple Java program that I'd like to run when I login and have it continue to run in the background (it's a server for a simple client/server program).

Currently I have a 'command' file that I'm using to start the process, but I have to keep doing it manually and it opens a Terminal window on the dock. I'm hoping there is a way to configure it to run in the background automatically when I log in.

Thanks guys!!!

You might want to look into Launchd. Although I am not sure if it supports Java, I see no reason why it wouldn't but do not know for sure.
 

larkost

macrumors 6502a
Oct 13, 2007
534
1
Cromulent is correct, using a Launchd agent is the right way of going.

And just a note: you want a daemon process, not thread. Threads aren't really daemons (python has "daemon threads", but that is a different concept).
 

stevejobbers

macrumors member
Original poster
Aug 19, 2008
80
0
thanks, i'll give it a shot.

is there a good resource online or tutorial? i tried some basic google searches but got kinda lost...
 

rrpalma

macrumors member
Sep 21, 2008
45
0
You might want to check Bruce Eckel's free on-line (and downloadable) book Thinking in Java. If I remember correctly, there's a couple of examples.

What you should basically consider doing thin infinite loop:

done = false;
while (!done) {
.....
//update done if necessary
}

then inside the loop, use a socket to hear for requests on a specific port, and when you get a request, create a new thread to service that request. Main thread keeps listening for new incoming requests.

Then use launchd to invoke this program via java "YourProgram"

Hope that helps.
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
You might want to check Bruce Eckel's free on-line (and downloadable) book Thinking in Java. If I remember correctly, there's a couple of examples.

What you should basically consider doing thin infinite loop:

done = false;
while (!done) {
.....
//update done if necessary
}

then inside the loop, use a socket to hear for requests on a specific port, and when you get a request, create a new thread to service that request. Main thread keeps listening for new incoming requests.

Then use launchd to invoke this program via java "YourProgram"

Hope that helps.

A launchd server process should quit when not in use so that infinite loop breaks best practice policy. Launchd handles the loading and unloading of server processes as and when they are required to conserve memory and CPU resources.
 

stevejobbers

macrumors member
Original poster
Aug 19, 2008
80
0
thanks for all the responses. the java program already runs in a loop, so that's not an issue. the only issue is trying to get the program to automatically start when i log in, and run in the background. everytime i google for launchd it says that i'll have to create plist files and other stuff.... i thought all i would have to do is add a command to some login script somewhere.

regardless, i found an application, Lingon, that seems to have worked for me. the process started up at login and continues to run in the background.

it's a free app, in case anyone else wants/needs to use it.
 

rrpalma

macrumors member
Sep 21, 2008
45
0
A launchd server process should quit when not in use so that infinite loop breaks best practice policy. Launchd handles the loading and unloading of server processes as and when they are required to conserve memory and CPU resources.

Very true. Just providing a quick and dirty solution :eek::eek:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.