问题
I'm trying to learn how to make different programs interact with each other, but finding information online on that has been impossible for me. I'm a self taught Flash/PHP programmer who has learned basic java recently.
Let´s assume there is a game coded in Java, and I have access to its open source. If I wanted to make a Frames Per Second display for it, I could easily do that now with my current knowledge. I'd open Eclipse, examine the code and find where to properly place the FPS counter, recompile the game and play with the new recompiled version.
That is not what I'm trying to learn now though. What I want to do is make a standalone .exe or .jar file that when I run it, it will display FPS in the game without me recompiling it. We see people doing that all the time. I'm playing new vegas now with a mod that opens an .exe file that displays an overlay map in the game.
However, I have no idea how to achieve something like that. What exactly is the logic behind such programs? What topic should I be reading to understand how to do that?
If the FPS example is too complex, I'll try a simpler one. I have a program that prints Hello World. I'd like to make another program that will run Hello World, but change the string to something else. Again, I don't even know where to begin.
Thank you
回答1:
The search keyword you are looking for is "IPC" (Inter-Process Communications). There are many solutions, see here and here for some starting points.
Most solutions involve some form of one or more of the following:
- Sockets
- Shared Memory
- Named Pipes (on platforms that support it)
- Storing data in files / databases
- Other automation (e.g. OLE on Windows, toolkit automation, etc.)
- More...
In your case a basic network socket based solution would be very straightforward (your game runs a basic server, your monitor application connects and periodically queries the frame rate, which your game provides; this allows both local and remote monitoring and involves a fairly minimal amount of code). There is some related search starting points at Java Interprocess Communication.
For your specific "hello world" example, that's actually a different case. That could be accomplished simply by having the printing program take a command line parameter that specified the string, and having the calling program execute it with appropriate command line parameters.
来源:https://stackoverflow.com/questions/22886213/java-make-one-program-interact-with-another