• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with startup

 
Adriano Ferreira
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adriano Ferreira
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting back your solution! That might be helpful if someone later has a similar problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic