交流两个Jar文件 [英] Communicate Two Jar File

查看:108
本文介绍了交流两个Jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是Java新手。我希望您会对这个问题感兴趣,但是请使您的答案尽可能明显。我将尝试以开放的方式告诉我的问题。
我正在使用NetBeans。我想交流两个jar文件,让我有两个jar A和B。它们都在处理中,也就是说,它们分别执行,并且我看到两个打开的窗口。在jar A的窗口中,它等待来自用户的字符串,我在此处写 call B。然后在jar B的窗口中,将说在...处调用了程序A。我在Internet上搜索,发现了这样的人员; java.util.jar.JarFile,远程方法调用,网络套接字等。但是我无法从他们那里得到任何结果,因为我找不到适合初学者的答案。因此,如果您建议其中一个,请给我至少一个小例子,说明如何使用它们。

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.

推荐答案

您可以使用java.net.socket在两个jar文件之间进行通信。

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.

在programA中,您将设置服务器套接字并侦听客户端连接

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);
}

如果服务器能够接受客户端,则clientSocket对象将具有远程地址和设置为客户端的远程端口。

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.

服务器成功与客户端建立连接后,它将使用以下代码与客户端通信(程序B):

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;
}

现在,您已经编写了程序A能够与程序对话的逻辑B,您还需要创建能够与之交互的客户端。

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.

程序B:

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

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

这篇关于交流两个Jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆