Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a java application which can be run as window service?
    primarykey
    data
    text
    <p>I need your advice on creating a Java application which can be used to run as a service. I am using Java service wrapper to install and run it as a service in windows. I have 2 classes ServiceApp.java &amp; RMIService.java. ServiceApp has main method and RMIService will be a server running to accept connection from client.. </p> <p>Now here is my problem. From my main method, as soon as I create RMIService, the main thread will exit and application halts which is correct. Since I wanted to block the main thread to run it as a service, I created a SocketServer and Socket object and called Socket.accept(). This works fine and service is running as main thread gets blocked but I am not convinced that this is the correct approach. Please provide me the right way to do this . Here is a code snippet :</p> <pre><code>public class ServiceApp { public static void main(String[] args) { System.out.println("Starting RMI server"); { RMIService rmiService; try { rmiService = new RemoteServerService(); Registry registry = LocateRegistry.createRegistry(1099); registry.rebind("RMIServer", rmiService); System.out.println("RMI server running"); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket( 7000 ); while ( true ) { socket = serverSocket.accept(); //This will block the main thread } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } </code></pre> <p>PS : I had searched Stackoverflow on these questions but couldn't find any that matched with my requirement.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload