tags:

views:

386

answers:

5

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?

+3  A: 

No, you can't.

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

J-16 SDiZ
+1  A: 

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

Some options:

  1. Create a tcp connection between the processes and send packets to the loopback interface.
  2. Use whatever native code interop java has (JNI?) to subscribe to custom messages (in the win32 sense of the word)
  3. Using native code, establish a named pipe between the two processes
  4. Read / write to a text file (not the best idea)

Hope this helps

Crippledsmurf
+4  A: 

You will need to create a network server, as explained by 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.
  • For very simple cases, you could also just create a file and poll it from the Java side.
  • You can also use named pipes: 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.

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

sleske
+8  A: 

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

  • 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
  • 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.
  • 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.
  • 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.
  • 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.

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.

nd
+1  A: 

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

  • System.in wrapped into a BufferedReader and calling readLine()
  • System.out.println() to write the responses

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

Basically the Windows equivalent of the Linux way of piping:

ls | grep java
kd304
That sure sounds elegant, but *how* do I write something from a Win32 app which can be read by my Java program from System.in?
DR
I once knew how to program the Win32 API but I don't remember precisely. When you create the run parameters to run the application using CreateProcess, you could then or later on call some other function to retrieve the input and output stream of the process you just created. Have a look at the documentation of CreateProcess and its neighboring functions in MSDN.
kd304