简体   繁体   English

将JAVA程序作为后台服务运行

[英]Run JAVA program as background service

Im doing an application in android,which connects to a PC via a Java server program using socket programming... 我正在用android做一个应用程序,该应用程序使用套接字编程通过Java服务器程序连接到PC。

I need to run the Java server program as a background service... 我需要将Java服务器程序作为后台服务运行...

can anybody pls help me??? 有人可以帮我吗?

 ServerSocket serverSocket = null;
      Socket socket = null;
      DataInputStream dataInputStream = null;
      DataOutputStream dataOutputStream = null;

      try {
       serverSocket = new ServerSocket(8888);
       System.out.println("Listening :8888");
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

      while(true){
       try {
        socket = serverSocket.accept();
        dataInputStream = new DataInputStream(socket.getInputStream());
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        System.out.println("ip: " + socket.getInetAddress());
        System.out.println("message: " + dataInputStream.readUTF());
        //dataOutputStream.writeUTF("Hello!");
        //String dataStream = dataInputStream.readUTF();
       // System.out.println("message: " + dataStream);
        //dataOutputStream.writeUTF("Hello!");
        c++;
        dataOutputStream.writeUTF(ss+c);
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
       finally{
        if( socket!= null){
         try {
          socket.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
        }

        if( dataInputStream!= null){
         try {
          dataInputStream.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
        }

        if( dataOutputStream!= null){
         try {
          dataOutputStream.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }

You will need to "wrap" your java application to run as a service. 您将需要“包装”您的java应用程序以作为服务运行。 This will allow the java program to execute just like other services. 这将允许Java程序像其他服务一样执行。 There are tools available for this purpose. 有一些工具可用于此目的。 See Java Service Wrapper and YAJSW for more details. 有关更多详细信息,请参见Java Service WrapperYAJSW

是的,您可以使用Java Service Wrapper运行Java应用程序。

尝试将Java代码设置为批处理文件;如果用于Windows操作系统...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM