2

Hi Friends, I am bit confuse about the JVM. First I want to know that Is each java programme (process) run in separate JVM or in the same JVM

e.g I am running two projects called ABC and DEF and both of them are starting with their own main method. First project have the class Test1(main Class) and the second project have the Test2(main class). Now I am trying to access some information of First project by starting the second project say Hashtable size, but when I am querying the size of Hashtable(of First project) in second project then its size is showing zero though before starting the first project I have added four element to the Hashtable. Can any one describe me why it is happening. What I am thinking that all the java process runs in their own JVM. Am I right! If Yes then how can I run two different java process into the same JVM. Thanks in anticipation.

Pranay Munshi
  • 71
  • 1
  • 5
  • you can consider using threads and have each thread run independently. i think hashtable does not support concurrency well and you should use hashmap instead; i'm not sure, you have to check that out. – happymeal Dec 26 '11 at 11:22

2 Answers2

0

You can have either running the two main methods in the same JVM or different JVMs. However unless your collection is exactly the same object or you are using a distributed collection, they will be two independent collections.

If you want the two pieces of code to change the collection, the simplest solution is have one program.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • my Collection object is static object(i.e my Hashtable). Still I am not able to decide whether two main classes (Which have main method) run in same JVM or in different JVM? If I run both of them together then will I be able to access subsequent class variables properly as like both are belongs the same project. If I'll use normal var. instead of Collection object then will it give proper result or not. – Pranay Munshi Dec 26 '11 at 13:54
  • If you use a shared Hashtable, in the same JVM, it should work as you need. – Peter Lawrey Dec 26 '11 at 20:56
  • >>Still I am not able to decide whether two main classes (Which have main method) run in same JVM or in different JVM?<< They run in different JVMs. – Binil Thomas Dec 05 '13 at 03:45
  • @user1057583 every time you run a new command from the command line, you are running a new program. There are environments which don't run the code in the command you start but in a shared process e.g. `scalac`, but Java doesn't do that by default. – Peter Lawrey Dec 05 '13 at 13:43
0

Well, it's depend on how you are implementing this kind of behavior, it seems to me that you are trying to share objects among application. I'm not an expert on this but you can pass by on here:

Here they use a JNDI approach which I think it's simpler and could guide you through what you want to do and I like the conclusion:

By building on the fundamentals of serialization and persistence, the JNDIHashtable allows you a little more flexibility in the realm of object persistence. You don't need to map the objects to database tables or host them via CORBA or EJB. And since the objects are stored remotely,

Community
  • 1
  • 1
Necronet
  • 6,704
  • 9
  • 49
  • 89