Home arrow static arrow Java Programming [Archive] - How to calling JVM in java code
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How to calling JVM in java code
This topic has 6 replies on 1 page.

Posts:39
Registered: 8/11/03
How to calling JVM in java code  
Jun 23, 2004 3:08 PM



 
Hello!
I continued my homework -- a java IDE written in java. I've asked how to compile the java code in my IDE here. Thanks to Franck_Lefevre, he showed me this:
//...

import com.sun.tools.javac.Main;

//...

public static boolean compileSource(String filename) {

String[] javac_args = { filename };

int result = Main.compile(javac_args);

return (result == 0);

}

now the problem is how to RUN the .class file on the JVM. Is there a method corresponding to this, ie also using java tools? or some better ways? I've tried a few ones but they don't work well.
I really appreciate you who helped me so much.
 

Posts:9,091
Registered: 8/15/01
Re: How to calling JVM in java code  
Jun 23, 2004 3:12 PM (reply 1 of 6)



 
With
Class c = Class.forName( "NameOfClassAssumingItIsInYourClassPath" );
c.getMethod( "main" ).invoke();

Your'll need to create a thread for it to live in, and a classloader for it if you want to load it more than once.
 

Posts:6,750
Registered: 1/25/04
Re: How to calling JVM in java code  
Jun 23, 2004 3:16 PM (reply 2 of 6)



 
Planetar, remember to read the API docs for the complete method signature for Class.getMethod.
 

Posts:9,091
Registered: 8/15/01
Re: How to calling JVM in java code  
Jun 23, 2004 3:19 PM (reply 3 of 6)



 
Planetar, remember to read the API docs for the
complete method signature for Class.getMethod.

Good point, take my code example as just a rough guide of what to look at in the API docs. Namly,Class, ClassLoader and Method.
 

Posts:39
Registered: 8/11/03
Re: How to calling JVM in java code  
Jun 23, 2004 3:25 PM (reply 4 of 6)



 
Thanks! And where should I check for the usage of Java Tools(tools.jar)?
 

Posts:39
Registered: 8/11/03
Re: How to calling JVM in java code  
Jun 23, 2004 3:57 PM (reply 5 of 6)



 
the new problem is: how to specify the classpath? I even add it to my environment varibles, but it still throw ClassNotFoundException.
 

Posts:39
Registered: 8/11/03
Re: How to calling JVM in java code  
Jun 23, 2004 4:03 PM (reply 6 of 6)



 
I guess ClassLoader will do it. Thank u guys!
 
This topic has 6 replies on 1 page.