Control java application from the outside

2

I created a java SE application that runs in the background, without swing or anything ... But how do I control this application? for example, in python applications we can execute commands (python something.py -c command1 -u command2), so I can have a control over the application, how would I do this with my java application?

And how can I control the application through another java application? For example: a panel made with swing with the options to pause the controlled application thread, continue, change configuration information, etc.

The application will run only on Linux.

    
asked by anonymous 27.05.2015 / 23:55

1 answer

1

One solution to your case might be the use of the ServerSocket class. In your background application you can leave as a server listening to a specific port. And in another application you can send commands through the socket, with this you can manipulate your application server knowing only the IP and port;

Server Side

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;  
import java.io.InputStreamReader;  

public class Server {  

    public static void main(String[] args) {  

        ServerSocket serv=null;  // Declara o socket 
        Socket s= null; // Socket de comunicação  

        BufferedReader entrada=null; //Leitor para a entrada de dados  

        try{  
            //Cria o ServerSocket na porta 9000   
            serv = new ServerSocket(9000);  
            //Aguarda uma conexão na porta especificada e cria   
            s = serv.accept();  
            //Cria um BufferedReader para o canal da stream de entrada de dados do socket  
            entrada = new BufferedReader(new InputStreamReader(s.getInputStream()));  

            //Aguarda por algum dado e imprime a linha recebida quando recebe  
            System.out.println(entrada.readLine());      

        }catch(IOException e){  
            System.out.println("Falha ao criar socket.");  
        }finally{  
            try{  
                //Encerra o socket  
                s.close();  
                //Encerra o ServerSocket  
                serv.close();  
            }catch(IOException e){  

            }  
        }  

}  

}

Client side

import java.io.IOException;  
import java.io.PrintStream;  
import java.net.Socket;  

public class Client {  

public static void main(String[] args){           
    Socket mSocket = null;  
    PrintStream mPrintStream = null;  

    try{  

    int x = 20;  
    mSocket = new Socket("<_ENDEREÇO_IP_>",9000);  
    mPrintStream = new PrintStream(mSocket.getOutputStream());  
    mPrintStream.println(x);  

    }catch(IOException e){                
    System.out.println("Falha ao enviar dados." + e.getMessage());  

    }finally{  
        try{  
            //Fecha o socket  
            mSocket.close();  
        }catch(IOException e){}  

    }  
}  
} 
    
28.05.2015 / 00:42