1

I have an application like the follows,

public class OpenApp {

    public static void main(String[] args) {
        if(args.length>0)
        System.out.println("Hi " + args[0]);
        System.in.read();    
    }

    public static String sayHi(){
        return "Hi";

    }

}

So The OpenApp will be running. I have some other methods. Can I call the sayHi method from another application, without creating new instance of the class.? Because I have some data constrains on running OpenApp.

Please Correct me If my question is wrong. Simply I'm trying to communicate between 2 JVM. So, I read, RMI is the best way to make the communication. So Is there any other way.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
Pasupathi Rajamanickam
  • 1,982
  • 1
  • 24
  • 48
  • No actually, trying to avoid specific communication protocols. Like Java reflections, I'm looking for. – Pasupathi Rajamanickam Oct 28 '14 at 07:07
  • 1
    @Pasupathi You **do** need some (at least interprocess) communication protocol if are `trying to communicate between 2 JVM`. See e.g. http://www.javaworld.com/article/2077419/learn-java/interprocess-communications-in-java.html. Other (non-network transparent, means local only) alternatives are [FIFO, Named Pipe or Shared Memory](http://stackoverflow.com/questions/1416780/java-interprocess-communication). – Andreas Fester Oct 28 '14 at 07:08
  • You can use `socket`, `RMI` or `websevices` to communicate between JMV. I feel `webservices` are the reliable and robus – G.S Oct 28 '14 at 07:13
  • Ya it's reliable. But, there's no challenging in that. Simply exposing webmethods and do those stuff. Socket communication is well enough to do this. But I need something interacting a java process while it's running. Kind of RnD only. Wanted to explore this kind of challenge. – Pasupathi Rajamanickam Oct 28 '14 at 07:18
  • you need to bind that module to your application where you want to use that method(sayHi)...thats the only way...if IPS is not preferable.. – Angad Tiwari Oct 28 '14 at 07:20
  • "trying to avoid specific communication protocols" - so, um... vague protocols, then? – Chris Martin Oct 28 '14 at 07:22
  • You could look at multi-JVM object caching but memory requirement for that would far outpace memory requirement for object creation! – Ironluca Oct 28 '14 at 07:23
  • Ya I'll check how to bind the application. – Pasupathi Rajamanickam Oct 28 '14 at 07:28
  • Maby you could take a look at JMX (http://en.wikipedia.org/wiki/Java_Management_Extensions). Depending on the size of the application you are trying to communicate with it something of a cannon-to kill-a-moskito type of solution though. – Apokralipsa Oct 28 '14 at 07:30

1 Answers1

1

For two applications to communicate, you need some form of Inter-Process Communication, IPC, pretty much by definition. So you need some kind of protocol. So, short answer to your question is: RMI is not the only way, but all other ways are similar, communication and not direct method call.

If you just want to call the method in code of another application, then add the .jar with the class to your application, or load the .jar at runtime, and (since that is a static method) just call the method. But this is just normal static method call, so you probably did not mean this?

Community
  • 1
  • 1
hyde
  • 60,639
  • 21
  • 115
  • 176