0

I'm searching for a communication between a Java application 'A' and another Java application 'B'. I`ve heard from JNI, Webservices and other stuff...

Current State: I've two separate Java Applications; At the moment i can start both manually and each of them is working very well. For each of the applications i`ve wrote a test-class to chek each function and all sideeffects...; Application B implements an interface, so i know each function @ B that i want to run! Each application uses the same interfaces for exchanging data (in future).

Problem: Now i want to start Application A (it starts ~5 Threads by it self). A should search, in a defined Path, all JAR Files and start one of them (selection via UI @ A). OK, this is not the big problem for me! My problem: I want to interact with the loaded "JAR File" (or better: with the application in the JAR File).

Question: How is an interaction possible by unsing simple tools or frameworks; A Webservice seems to be not dynamic enought (I`m not that expert in Webservices, jet!)!?

Important for me are following points:

  • Fast Communication
  • Dynamic Loading of an external library (eq. the other Java application = B.) is required
  • Package names shouldn`t be importat (i want to be open for other developers)
  • Open for other Developers (they just have to implement my interface)

More specific

  • A is a Controller / server /etc; runs more or less @ background
  • B (ore all other implementations of my interface) is a Game; startet from A, controlled from A and stopped from A
  • You could load the classes from the jar using Reflection. – MightyPork Aug 08 '13 at 13:59
  • If you just want to run code in a JAR, you can load it into A and call it directly. – Peter Lawrey Aug 08 '13 at 14:03
  • @MightyPork: Nice idea, do you have an example code !? – kallinger0815 Aug 08 '13 at 14:05
  • No, sorry. But it shouldn't be too hard to google something. – MightyPork Aug 08 '13 at 14:06
  • Do your A and B applications depend on 3-rd party libraries? Can you guarantee that all "B" applications depends on the same versions of the common libraries and on the same versions as application A? – Olaf Aug 08 '13 at 14:07
  • @PeterLawrey: How would it work? I know how to run a JAR File form an other JAVA Programm, but what do you mean with "load it" and "call it directly"? I want to call mehtods @ appl. B; next time i want to start JAR D and call methods there (B and D are implementing the same interface) – kallinger0815 Aug 08 '13 at 14:08
  • @Olaf: No! A is my application; B is one of a friend of mine, but in future it could be the lib of each developer (just implement my interface) – kallinger0815 Aug 08 '13 at 14:09
  • Run program A with all the JARs you need. Code in one JAR calls classes in another JAR like it was in the same program, because now it is. – Peter Lawrey Aug 08 '13 at 14:10
  • @kallinger0815 It doesn't matter who wrote the JARs. It not clear why you want to be able to pretend the programs were one, when you can actually have just one program. – Peter Lawrey Aug 08 '13 at 14:11
  • @PeterLawrey: Looks like this is not the case. OP has to run applications in separate JVMs to allow for different versions of 3rd party libraries. Using OSGi or writing custom class loaders would be an overkill for that. – Olaf Aug 08 '13 at 14:13
  • @PeterLawrey: I`m going more specific: Programm B (or all other implementations of my interface) are Games. A is the "Controller/Server/... how ever you want to call it". A starts the selected Game and calls Methods @ B ... – kallinger0815 Aug 08 '13 at 14:14
  • In short, B must be running whether A is running or not? In that case I would use RMI given you keep mentioning methods. You can also use messaging like JMS, or JMX (which is based on RMI) or use a plain Socket. It is up to you to decide what you want. – Peter Lawrey Aug 08 '13 at 14:25
  • Yes! A is always running, B will be startet from A and stopped from A. Ok, i`ll asking google for: JMS, JMX, RMI, Reflection, ... Additional terms!? – kallinger0815 Aug 08 '13 at 14:27
  • regarding loadinging B into A: http://stackoverflow.com/questions/13504120/load-jar-dynamically - e.g. Eclipse, Minecraft, .. load jars as plugins, plugins implement an interface A calls methods. B is not executed as a separate app, more like a dynamically loaded library – zapl Aug 08 '13 at 14:33

2 Answers2

0

After reading all clarifications, I propose to run the "controller" application in a separte JVM and let it start "games" in separate JVMs using shell commands.

If the further communication between the "controller" and "games" is required, the simplest technologies for the interprocess communications are sockets and Java RMI.

A technology is available to allow cohabitation of the "controller" and "games" in the same JVM, even if they use different versions of the same libraries. It also allows for dynamic loading and unloading of modules without restarting JVM. This technology is OSGi but, in my opinion, mastering it requires a steeper learning curve than using shell, sockets and/or Java RMI.

Olaf
  • 6,249
  • 1
  • 19
  • 37
0

I did it like this:

  • Create an Interface for your Game with all Methods you have to use and you would provide
  • Code the game.class which implements the interface

  • Generate a new Object of type game (Game g = null;)

  • Where you have to make the instance you can write: g = new Game();
  • I´ve checked the type with "instanceof"

  • Code will following soon..