Calling a Java program from another

How to invoke a Java command from a separate java program.

I understand that Runtime.getRuntime (). exec ("cmd c / javac <>. java"); should work. However, this will be platform specific.

Any other APIs available that could make it work in j2sdk1.4?

+1


a source to share


4 answers


If you can run everything in one JVM, you can do something like this:

public class Launcher {
    ...
    public static void main(String[] args) throws Exception {
        launch(Class.forName(args[0]), programArgs(args, 1));
    }

    protected static void launch(Class program, String[] args) throws Exception {
        Method main = program.getMethod("main", new Class[]{String[].class});
        main.invoke(null, new Object[]{args});
    }

    protected static String[] programArgs(String[] sourceArgs, int n) {
        String[] destArgs = new String[sourceArgs.length - n];
        System.arraycopy(sourceArgs, n, destArgs, 0, destArgs.length);
        return destArgs;
    }

      



And run it using the command line like this:

java Launcher OtherClassWithMainMethod %CMD_LINE_ARGS%

      

+3


a source


Call Runtime.getRuntime (). exec () is not only platform specific but also extremely inefficient. This will result in a new shell and all jvm which could potentially be very expensive depending on the dependencies of that application (no pun intended).

The best way to execute "external" Java code is to place it in the CLASSPATH. If you must call the main method of the application, you can simply import and call the method directly. You can do it like this:



import my.externals.SomeMain

// call as if we are running from console
SomeMain.main(new String[] {"some", "console", "arguments"})

      

Of course, the best option would be to just use this as an external library and access the code you want without calling SomeMain.main (). Adhering to best practices and creating proper encapsulated modular objects can greatly improve portability and ease of use when used by other applications.

+2


a source


When you leave the JVM and go to system commands, you have to deal with platform-specific commands. The JVM offers a good abstraction, so why leave?

If you want to execute java-specific binaries check out the ant java libraries. You can execute ant scripts from java that execute platform specific commands.

0


a source


Java programming by quercus php on GAE:

import com.newatlanta.commons.vfs.provider.gae.GaeVFS;
import org.apache.commons.io.IOUtils;
import java.lang.Long;
import java.lang.Boolean;
GaeVFS::setRootPath(quercus_servlet_request()->getSession(true)->getServletContext()->getRealPath('/'));
define('VFSM', GaeVFS::getManager());

//VFSM->resolveFile('gae://gaevfs')->createFolder();
$file=VFSM->resolveFile('gae://gaevfs/tmp1');
//$file->createFile();

$text='pp';
$method=$file->getClass()->getDeclaredMethod('updateContentSize', array(Long::TYPE, Boolean::TYPE));
$method->setAccessible(true);
$method->invoke($file, strlen($text), true);
$out=$file->getContent()->getOutputStream();
IOUtils::write($text, $out, 'UTF8');
$out->close();

$in=$file->getContent()->getInputStream();
$method=$file->getClass()->getDeclaredMethod('doGetContentSize',array());
$method->setAccessible(true);
$len=$method->invoke($file);
$whole=IOUtils::toString($in, 'UTF8').':'.$len."<br>";
$in->close();
echo $whole;

GaeVFS::clearFilesCache();
GaeVFS::close();

      

0


a source







All Articles