File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Help with startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with startup" Watch "Help with startup" New topic
Author

Help with startup

Adriano Ferreira
Ranch Hand

Joined: Jan 05, 2011
Posts: 34
Hello, I wonder if I can make my application run only once, even when it is initialized by the startup of windows. I need that because if my server suddenly shut down the application starts automatically with your computer, and it is possible that it did not open again at startup of windows if another person to log on the server.
I tried with lock a port but it did not runs, I'm trying now use the class Socket for it, check it



eu nao consigo abrir a minha aplicacao porque sempre ocorre o seguinte erro:
Connection refused: connect
Estou no aguardo
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

Welcome to JavaRanch.

One way to check if a copy of your program is already running is indeed by opening a socket, but you should use ServerSocket for that, not a regular Socket.

What you should do is create a ServerSocket and make it listen on a specific port (for example 7000 or 7800, or whatever number you want to use) by calling accept() on the ServerSocket. If there is another process already listening on that port, you will get an error (something like "port already in use"), which indicates that there is probably already another copy of your program running on the server.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Adriano Ferreira
Ranch Hand

Joined: Jan 05, 2011
Posts: 34
Jesper de Jong wrote:Welcome to JavaRanch.

One way to check if a copy of your program is already running is indeed by opening a socket, but you should use ServerSocket for that, not a regular Socket.

What you should do is create a ServerSocket and make it listen on a specific port (for example 7000 or 7800, or whatever number you want to use) by calling accept() on the ServerSocket. If there is another process already listening on that port, you will get an error (something like "port already in use"), which indicates that there is probably already another copy of your program running on the server.


Thank you very much for the answer
I will try it
Adriano Ferreira
Ranch Hand

Joined: Jan 05, 2011
Posts: 34
Adriano Ferreira wrote:
Jesper de Jong wrote:Welcome to JavaRanch.

One way to check if a copy of your program is already running is indeed by opening a socket, but you should use ServerSocket for that, not a regular Socket.

What you should do is create a ServerSocket and make it listen on a specific port (for example 7000 or 7800, or whatever number you want to use) by calling accept() on the ServerSocket. If there is another process already listening on that port, you will get an error (something like "port already in use"), which indicates that there is probably already another copy of your program running on the server.


Thank you very much for the answer
I will try it


Thanks for having me on the forum, I would like to thank you for the tip, I managed to do what I wanted, thank you
Here's the code of the class:

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

Thanks for posting back your solution! That might be helpful if someone later has a similar problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with startup