Prashant Shilimkar
Prashant Shilimkar

Reputation: 8820

How to create instance of already loaded class by other application in same JVM?

I have created two small java applications in Netbeans IDE, Say

App1 have class First as follow

public class First{

  public static void main(String... str){
    First f = new First();
    System.out.print(f.getValue());
  }

  public int getValue(){
    return 10;
  }
}

And App2 have class Second as follow

public class Second {
  public static void main(String... str){
    try{
      Class myClass = Class.forName("App1.First");
      Method m = myClass.getDeclaredMethod("getValue",new Class[] {});
      Object result = m.invoke(myClass,null);
      System.out.println(result);
    }catch(Exception e){
      System.out.println(e);
    }
  }
}

I have Run App1.First in Netbeans and after that i have run App2.Second in Netbeans.

OUTPUT: java.lang.ClassNotFoundException: App1.First

Code above is not working. I want to Access getValue method in App1.First from App2.second.

I want to know whether we can create instance of class from other application and execute its methods on it in Java? If yes, then How?
Consider SecurityManager is not running.Please let me know where i have done mistake in code OR understanding concept.

Your response will be greatly appreciated.

Thanks for all the responses. If Netbeans runs each app in different JVM then May i know How we can run an Application in JVM in which other application is already running?

Upvotes: 0

Views: 281

Answers (4)

user3099347
user3099347

Reputation:

Each app when you run using java command, it starts in different JVM. so you cannot load class using this way in the same JVM

Upvotes: 1

Sergey Morozov
Sergey Morozov

Reputation: 4608

First. When you start 2 different programm, you have 2 differenc instances of JVM. Each JVM not know nothing about another.

Second. Exception is occurs when class is not contained in classpath. You must add dependencies App1 for project App2 i.e. put classes from App1 to classpath App2.

Upvotes: 1

Dark Knight
Dark Knight

Reputation: 8347

If App1 and App2 are two different application/ projects, App2 will not see APP1's classes unless included in classpath.

Upvotes: 1

yshavit
yshavit

Reputation: 43391

No, you can't.

Each application has its own JVM, with its own class path, loaded classes, etc. You can't (within pure Java) access one JVM's classes directly from another class.

What you'd need to do is to put App1's classes on the classpath for App2 -- there are tools such as maven that help you set these dependencies up. If you do that, both JVMs will have a separate copy of each class; it's up to you to make sure that both copies are identical (for instance, by making sure you redeploy App2 every time you redeploy App1). You'll also need to set up some way for the JVMs to communicate to each other: set up a socket or some similar approach, with a serialization format (serialized classes, protobufs, etc) that both agree on.

Upvotes: 2

Related Questions