public static VirtualMachine createVirtualMachine(VirtualMachineConfiguration cfg) throws VirtualMachineInstantiationException, SecurityException {
        try {
            File java = new File(System.getProperty("java.home"), "bin" + File.separator + "java");
            String program;
            if (cfg.getMainClassName() != null) {
                program = cfg.getMainClassName();
            }
            else if (cfg.getJarFile() != null) {
                program = "-jar " + cfg.getJarFile();
            }
            else
                throw new IllegalArgumentException("main class or jar file must be defined");
            
            String[] args = cfg.getArguments();
            StringBuffer argString = new StringBuffer();
            if (args != null)
                for (int i = 0; i < args.length; i++) {
                    argString.append(' ');
                    argString.append(args[i]);
                }
            
            StringBuffer javaArgs = new StringBuffer();
            
            File[] classPath = cfg.getClassPath();
            if (classPath != null && classPath.length > 0) {
                javaArgs.append("-cp");
                for (int i = 0; i < classPath.length; i++) {
                    javaArgs.append(' ');
                    javaArgs.append(classPath[i]);
                }
            }
            
            final Process p = Runtime.getRuntime().exec(java + " " + javaArgs + " " + program + " " + argString);
            return new VirtualMachine() {
                public InputStream getInputStream() {
                    return p.getInputStream();
                }

                
                public InputStream getErrorStream() {
                    return p.getErrorStream();
                }

                
                public OutputStream getOutputStream() {
                    return p.getOutputStream();
                }
                
                
                public void waitFor() throws InterruptedException {
                    p.waitFor();
                }
                
                
                public void destroy() {
                    p.destroy();
                }
            };
        }
        catch (Exception e) {
            throw new VirtualMachineInstantiationException(e.toString());
        }            
    }