Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running multiple applications using a single VM

 
Jander
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone
Does anyone know whether i can run say, 10 different (in logic scope) java apps without launching 10 instances of the VM?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have one driver program that launches the other programs using

Runtime.getRuntime().exec("java myClass");
This thread has an example: http://www.javaranch.com/ubb/Forum33/HTML/000194.html
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yeah, and Jander:

The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at "JavaRanch Naming Policy" . We require names to have at least two words (not just letters), separated by a space, and strongly recommend that you use your full real name. We want to get to know you as a Professional. Please choose a new name which meets the requirements and re-register.
Cindy
 
Jander
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes ma'am

Originally posted by Cindy Glass:
Oh yeah, and Jander:

The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at [b]"JavaRanch Naming Policy"
. We require names to have at least two words (not just letters), separated by a space, and strongly recommend that you use your full real name. We want to get to know you as a Professional. Please choose a new name which meets the requirements and re-register.
Cindy[/B]

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think Cindy's suggestion actually achieves what you are asking for. Using Runtime.exec() like that will start a new JVM for each command - the same as if you had started them manually.
If your "applications" really are separate, then running them in the same VM is basically a matter of putting their classes and jars in the classpath, and executing the main method of their entry point class in a new thread:

If you are in the unlucky situation of each application needing a separate copy of certain static variables or singletons, then you will have to create your own classloader to load each application.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it thanks BTW, when you are talking about a classloader are you refering to the use of the reflection library?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sort of. A classloader is a Java object which manages the loading of classes. Typically classes are loaded from class files on a filesystem, or from a jar file, or from a URL (think applets), but it is possible to write your own classloader to load classes from anywhere or from any format.
Common custom classloader implementations include reload-if-modified classloaders as used in JSP containers and some test harnessses etc.
 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep Jander!
you can run any number of java programs in a Single VM using ClassLoader objects.
Today only I've read about this topic, so got very excited to reply this topic.
But keep in mind that, you cannot create an instance of ClassLoader from the Java API. Its an Abstract Class.
The mostly used ClassLoader for these purposes is...URLClassLoader
With which you can load any class from anywhere.
Cheers
Siva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic