如何向正在运行的Java程序发送命令?

时间:2021-07-09 20:50:00

I have a Java program and I want to send a command from my Win32 application. Normally I'd use WM_COPYDATA but what options do I have with Java?

我有一个Java程序,我想从我的Win32应用程序发送命令。通常我会使用WM_COPYDATA,但我对Java有哪些选择?

5 个解决方案

#1


There are some ways to interoperate between Java and Windows. Ordered in power and difficulty:

有一些方法可以在Java和Windows之间进行互操作。命令权力和困难:

  • For handling window messages, you could use Jawin - it even features a demo of how to handle window messages - or something similar. Of course, if you bind your Java program to a library like Jawin, it will never run on a non-Windows machine
  • 对于处理窗口消息,您可以使用Jawin - 它甚至包含如何处理窗口消息的演示 - 或类似的东西。当然,如果将Java程序绑定到像Jawin这样的库,它将永远不会在非Windows机器上运行

  • For simple interaction between Win32 and Java, a socket bound to listen only on localhost would be my favourite choice. The protocol may be simple, but I would prefer a plain text protocol for easier debugging. Be aware that a socket connection can break down if the user terminates a program.
  • 对于Win32和Java之间的简单交互,限制在localhost上侦听的套接字将是我最喜欢的选择。协议可能很简单,但我更喜欢纯文本协议以便于调试。请注意,如果用户终止程序,套接字连接可能会中断。

  • You can use (local) web services, like suggested in other posts here. On both sides make sure that you use your Webservice/XML libaries to construct and parse the messages, it is far too easy to construct malformed XML if you do string concatenation.
  • 您可以使用(本地)Web服务,如此处其他帖子中所建议的那样。双方都要确保使用Webservice / XML库来构造和解析消息,如果你进行字符串连接,那么构造格式错误的XML就太容易了。

  • You can put the functionality of your Windows program into a COM component and use a Java-to-COM bridge: Jacob or j-Interop are popular free libaries for this, j-Integra seems a popular choice for businesses with legacy systems.
  • 您可以将Windows程序的功能放入COM组件并使用Java-to-COM桥接器:Jacob或j-Interop是流行的免费库,j-Integra似乎是具有遗留系统的企业的流行选择。

  • You can put the functionality of your Java program into a COM component and use Sun's Java-ActiveX bridge. From my personal experience, this is a rather awkward option: Development of the Java-ActiveX bridge has stalled since 1.4, the installation of the ActiveX causes your Java component to be installed somewhere in the JRE directory and debugging your Java components inside the ActiveX container is rather cumbersome.
  • 您可以将Java程序的功能放入COM组件中,并使用Sun的Java-ActiveX桥。根据我的个人经验,这是一个相当尴尬的选择:Java-ActiveX桥的开发自1.4开始就停滞不前,ActiveX的安装导致您的Java组件安装在JRE目录中的某个位置并调试ActiveX容器内的Java组件相当麻烦。

Sidenote: if you are dealing with strings on both sides, always take into account that Java handles strings as something quite different from byte arrays. Especially if you are using Windows ANSI strings be aware that the characters 81, 8D, 8F, 90, and 9D are specified as undefined in the Windows-1252 codepage, therefore Java will produce question marks or exceptions if your Windows strings contain these elements. Therefore, if at all possible, use WChar strings on the Windows side or restrict yourself to safe characters.

旁注:如果您要处理双方的字符串,请始终考虑Java将字符串处理为与字节数组完全不同的字符串。特别是如果您使用的是Windows ANSI字符串,请注意在Windows-1252代码页中将字符81,8D,8F,90和9D指定为未定义,因此如果Windows字符串包含这些元素,Java将产生问号或异常。因此,如果可能的话,在Windows端使用WChar字符串或限制自己使用安全字符。

#2


You will need to create a network server, as explained by J16 SDiZ.

您将需要创建一个网络服务器,如J16 SDiZ所述。

  • One simple way is to use XML-RPC. There are ready-made libraries for Java and just about any other language, and it's simple. We use it in our app. But really, any network protocol will do.
  • 一种简单的方法是使用XML-RPC。有现成的Java库和几乎任何其他语言,而且很简单。我们在我们的应用中使用它。但实际上,任何网络协议都可以。

  • For very simple cases, you could also just create a file and poll it from the Java side.
  • 对于非常简单的情况,您也可以只创建一个文件并从Java端进行轮询。

  • You can also use named pipes: http://www.coderanch.com/t/328057/Java-General-advanced/java/Use-Named-Pipe-IPC-between
  • 您还可以使用命名管道:http://www.coderanch.com/t/328057/Java-General-advanced/java/Use-Named-Pipe-IPC-between

  • Then there's also RMI, but that is probably overkill for your (simple) purpose. Finally, you could use JNI to directly access Window's native communication mechanism.
  • 然后还有RMI,但这可能是你的(简单)目的的过度杀伤。最后,您可以使用JNI直接访问Window的本机通信机制。

Personally, I'd use XML-RPC or some other simple, standardized protocol.

就个人而言,我会使用XML-RPC或其他一些简单的标准化协议。

#3


No, you can't.

不,你不能。

You have to create a network server and listen to a (local) socket. Or, alternatively, use JNI.

您必须创建网络服务器并侦听(本地)套接字。或者,使用JNI。

#4


Java and win32 bith implment a lot of the same technologies so having the two applications communicating is not impossible, one just needs to pick a transport and protocol common to both applications

Java和win32 bith实现了许多相同的技术,因此让两个应用程序进行通信并非不可能,只需要选择两个应用程序共有的传输和协议

Some options:

  1. Create a tcp connection between the processes and send packets to the loopback interface.
  2. 在进程之间创建tcp连接,并将数据包发送到环回接口。

  3. Use whatever native code interop java has (JNI?) to subscribe to custom messages (in the win32 sense of the word)
  4. 使用任何本机代码互操作java(JNI?)订阅自定义消息(在win32意义上的单词)

  5. Using native code, establish a named pipe between the two processes
  6. 使用本机代码,在两个进程之间建立命名管道

  7. Read / write to a text file (not the best idea)
  8. 读/写文本文件(不是最好的主意)

Hope this helps

希望这可以帮助

#5


You could use the command line I/O streams to send in commands and retrieve the answers:

您可以使用命令行I / O流发送命令并检索答案:

  • System.in wrapped into a BufferedReader and calling readLine()
  • System.in包装到BufferedReader并调用readLine()

  • System.out.println() to write the responses
  • System.out.println()写入响应

I guess Win32 has the methods to capture the I/O streams of the application you start.

我想Win32有捕获你启动的应用程序的I / O流的方法。

Basically the Windows equivalent of the Linux way of piping:

基本上Windows相当于Linux的管道方式:

ls | grep java

#1


There are some ways to interoperate between Java and Windows. Ordered in power and difficulty:

有一些方法可以在Java和Windows之间进行互操作。命令权力和困难:

  • For handling window messages, you could use Jawin - it even features a demo of how to handle window messages - or something similar. Of course, if you bind your Java program to a library like Jawin, it will never run on a non-Windows machine
  • 对于处理窗口消息,您可以使用Jawin - 它甚至包含如何处理窗口消息的演示 - 或类似的东西。当然,如果将Java程序绑定到像Jawin这样的库,它将永远不会在非Windows机器上运行

  • For simple interaction between Win32 and Java, a socket bound to listen only on localhost would be my favourite choice. The protocol may be simple, but I would prefer a plain text protocol for easier debugging. Be aware that a socket connection can break down if the user terminates a program.
  • 对于Win32和Java之间的简单交互,限制在localhost上侦听的套接字将是我最喜欢的选择。协议可能很简单,但我更喜欢纯文本协议以便于调试。请注意,如果用户终止程序,套接字连接可能会中断。

  • You can use (local) web services, like suggested in other posts here. On both sides make sure that you use your Webservice/XML libaries to construct and parse the messages, it is far too easy to construct malformed XML if you do string concatenation.
  • 您可以使用(本地)Web服务,如此处其他帖子中所建议的那样。双方都要确保使用Webservice / XML库来构造和解析消息,如果你进行字符串连接,那么构造格式错误的XML就太容易了。

  • You can put the functionality of your Windows program into a COM component and use a Java-to-COM bridge: Jacob or j-Interop are popular free libaries for this, j-Integra seems a popular choice for businesses with legacy systems.
  • 您可以将Windows程序的功能放入COM组件并使用Java-to-COM桥接器:Jacob或j-Interop是流行的免费库,j-Integra似乎是具有遗留系统的企业的流行选择。

  • You can put the functionality of your Java program into a COM component and use Sun's Java-ActiveX bridge. From my personal experience, this is a rather awkward option: Development of the Java-ActiveX bridge has stalled since 1.4, the installation of the ActiveX causes your Java component to be installed somewhere in the JRE directory and debugging your Java components inside the ActiveX container is rather cumbersome.
  • 您可以将Java程序的功能放入COM组件中,并使用Sun的Java-ActiveX桥。根据我的个人经验,这是一个相当尴尬的选择:Java-ActiveX桥的开发自1.4开始就停滞不前,ActiveX的安装导致您的Java组件安装在JRE目录中的某个位置并调试ActiveX容器内的Java组件相当麻烦。

Sidenote: if you are dealing with strings on both sides, always take into account that Java handles strings as something quite different from byte arrays. Especially if you are using Windows ANSI strings be aware that the characters 81, 8D, 8F, 90, and 9D are specified as undefined in the Windows-1252 codepage, therefore Java will produce question marks or exceptions if your Windows strings contain these elements. Therefore, if at all possible, use WChar strings on the Windows side or restrict yourself to safe characters.

旁注:如果您要处理双方的字符串,请始终考虑Java将字符串处理为与字节数组完全不同的字符串。特别是如果您使用的是Windows ANSI字符串,请注意在Windows-1252代码页中将字符81,8D,8F,90和9D指定为未定义,因此如果Windows字符串包含这些元素,Java将产生问号或异常。因此,如果可能的话,在Windows端使用WChar字符串或限制自己使用安全字符。

#2


You will need to create a network server, as explained by J16 SDiZ.

您将需要创建一个网络服务器,如J16 SDiZ所述。

  • One simple way is to use XML-RPC. There are ready-made libraries for Java and just about any other language, and it's simple. We use it in our app. But really, any network protocol will do.
  • 一种简单的方法是使用XML-RPC。有现成的Java库和几乎任何其他语言,而且很简单。我们在我们的应用中使用它。但实际上,任何网络协议都可以。

  • For very simple cases, you could also just create a file and poll it from the Java side.
  • 对于非常简单的情况,您也可以只创建一个文件并从Java端进行轮询。

  • You can also use named pipes: http://www.coderanch.com/t/328057/Java-General-advanced/java/Use-Named-Pipe-IPC-between
  • 您还可以使用命名管道:http://www.coderanch.com/t/328057/Java-General-advanced/java/Use-Named-Pipe-IPC-between

  • Then there's also RMI, but that is probably overkill for your (simple) purpose. Finally, you could use JNI to directly access Window's native communication mechanism.
  • 然后还有RMI,但这可能是你的(简单)目的的过度杀伤。最后,您可以使用JNI直接访问Window的本机通信机制。

Personally, I'd use XML-RPC or some other simple, standardized protocol.

就个人而言,我会使用XML-RPC或其他一些简单的标准化协议。

#3


No, you can't.

不,你不能。

You have to create a network server and listen to a (local) socket. Or, alternatively, use JNI.

您必须创建网络服务器并侦听(本地)套接字。或者,使用JNI。

#4


Java and win32 bith implment a lot of the same technologies so having the two applications communicating is not impossible, one just needs to pick a transport and protocol common to both applications

Java和win32 bith实现了许多相同的技术,因此让两个应用程序进行通信并非不可能,只需要选择两个应用程序共有的传输和协议

Some options:

  1. Create a tcp connection between the processes and send packets to the loopback interface.
  2. 在进程之间创建tcp连接,并将数据包发送到环回接口。

  3. Use whatever native code interop java has (JNI?) to subscribe to custom messages (in the win32 sense of the word)
  4. 使用任何本机代码互操作java(JNI?)订阅自定义消息(在win32意义上的单词)

  5. Using native code, establish a named pipe between the two processes
  6. 使用本机代码,在两个进程之间建立命名管道

  7. Read / write to a text file (not the best idea)
  8. 读/写文本文件(不是最好的主意)

Hope this helps

希望这可以帮助

#5


You could use the command line I/O streams to send in commands and retrieve the answers:

您可以使用命令行I / O流发送命令并检索答案:

  • System.in wrapped into a BufferedReader and calling readLine()
  • System.in包装到BufferedReader并调用readLine()

  • System.out.println() to write the responses
  • System.out.println()写入响应

I guess Win32 has the methods to capture the I/O streams of the application you start.

我想Win32有捕获你启动的应用程序的I / O流的方法。

Basically the Windows equivalent of the Linux way of piping:

基本上Windows相当于Linux的管道方式:

ls | grep java