I was searching details about the JVM. How JVm call main() method, how i can execute multple Java file into a single JVM process.
How to excute multiple program into a single JVM process.. Pravin Mangalam
Collapse
X
-
Tags: None
-
The java compiler has already compiled the java program (including the main method) into bytecode. Now the JVM loads this bytecode, searches for the main() method and executes it to start the program.How JVm call main() method,
Just load the java file (classloader) and execute one of its method in the main thread or in a new created thread (Thread.run()). Or write a static initializer block inside your java file that will execute a method of your java file. Static initializers are executed immediately after a class is loaded.how i can execute multple Java file into a single JVM process. -
-
For what do you need an example (that you cannot find by google) ?
For controlling the class loader? For writing your own java compiler regarding compilation of the main method?
For writing a multi-threaded program? How to write a static initializer block?
Please be more specific. The problem with the general stuff is that I could write 20 pages or more for help, but it may not contain the help you need for your special case.
The best way is: you list the code here that you already have written yourself and tell us where you are stuck, and we can help you out in this specific case.Comment
-
let me explain My problem...
cmd>java Test
My above statement excute one JVM and there will be one main thread. ok
Now
cmd>java Test2
this statement also execute another JVM which occupy another memory space and main thread.
Now my Questoin : i want to execute Test2 java program into the First Running JVM. this save my memory and time also which taken for activating a new JVM.
it means in a single JVM how can ii create multiple main thread..Comment
-
Thanks for your explanation. That makes it much clearer and I am glad to give you a helpful answer now. What you need is the knowledge about threads. You can read about it in detail for example if you google for "java simple thread example".
In your case, you should rename the methods "main" in both your files Test.java and Test2.java to "run()" and add "extends Thread" to both classes.
Now you will write a new file, let us call it "ThreadStarter" , which you will run from the command line (shell) with "java ThreadStarter".
By the way, if you want to keep the threads running after ThreadStarter and the shell finishes, read about "Demon" threads.Code:class ThreadStarter { public static void main (String[] args) { new Test().start(); new Test1().start(); } }Comment
Comment