Communicate Two Jar File [closed]

瘦欲@ 提交于 2020-01-07 15:19:06

问题


First of all I am new in Java. I hope that you would interest on that question, but please make your answers as obvious as possible. And I will try to tell my problem in an open way. I am using NetBeans. I want to communicate two jar files such a way that,lets i have two jars A and B. And they are both in process, that is they are executed separately and I see two opened window. In the window of jar A, it waits for a string from user and I write there "call B". Then in the window of jar B, it will say that "program A is called at ... ". I searched on Internet and I have found such staff;"java.util.jar.JarFile, Remote Method Invocation, Network Sockets etc". But I could not reach any result from them since i could not find understandable answers for beginners. So if you suggest one of them, please give me at least a small example showing how to use them.


回答1:


For a simple form of communication you could use java.net.socket to communicate between the two jar files. This will allow you to have the "ping pong" effect you desire.

In programA you would setup the server socket and listen for a client to connect

try {
serverSocket = new ServerSocket(4567);
} 
catch (IOException e) {
    System.out.println("Could not listen on port: 4567");
    System.exit(-1);
}
Socket clientSocket = null;
try {
    clientSocket = serverSocket.accept();
} 
catch (IOException e) {
    System.out.println("Accept failed: 4567");
    System.exit(-1);
}

If the server is able to accept a client the clientSocket object will have the remote address and remote port set to that of the client.

After the server successfully establishes a connection with a client, it communicates with the client (program B) using this code:

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = 
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;

out.println("ping");

while ((inputLine = in.readLine()) != null) {   
    System.out.println("Program B says: " + inputLine);
    out.println("server: " + inputLine);
    if (inputLine.equals("quit"))
        break;
}

Now you have written the logic for program A to be able to talk to program B you also need to create the client to be able to interact with it.

ProgramB:

Socket socket = new Socket("programb", 4567);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader sysIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;


while ((fromServer = in.readLine()) != null) {
    System.out.println("Server: " + fromServer);
if (fromServer.equals("server: quit"))
    break;

fromUser = sysIn.readLine();
if (fromUser != null) {
    System.out.println("Client: " + fromUser);
    out.println(fromUser);
}

}

Then for good practice close the streams

out.close();
in.close();
sysIn.close()
socket.close();

And this should give you a basis to be able to talk between the two, you will probably need to implement some form of protocol (something to handle messages) on the server so that it outputs something useful

Reference: http://docs.oracle.com/javase/6/docs/api/java/net/Socket.html



来源:https://stackoverflow.com/questions/13152972/communicate-two-jar-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!