How do I invoke the command-line Java interpreter from a web server or servlet (assuming there is a java application on the machine where the webserver is located)? Or, can I invoke Java with CGI?

Serge Knystautas

First you should consider how you want to run your command-line Java interpreter. Do you want this to happen within the same Java Virtual Machine (JVM), i.e., the same memory space, that the servlet is running or do you want it to be a separate process?

If you want to run it within the JVM, you can call the static main() method on the class you were going to run on the command-line. You would create a String array with whatever command-line arguments you were going to pass, and pass this to the main() method.

If you want to run this in a separate JVM, you would use the java.lang.Runtime.exec() method. You would use the exec() method to run the new command-line and more than likely, wait for the command-line program to finish executing. Here is some sample code:

Runtime rt = Runtime.getRuntime();
Process process = rt.exec("java MyClass arg1 arg2");
try {
} catch (InterruptedException ie) {
[ See also ]

[ You can also bypass servlets altogether using the JavaCGI package. -Alex]

0 Comments  (click to add your comment)
Comment and Contribute









About | Sitemap | Contact