private static String fork(String sjavac, String portfile, String logfile, int poolsize, int keepalive,
            final PrintStream err, String stdouterrfile, String background)
            throws IOException, ProblemException {
        if (stdouterrfile != null && stdouterrfile.trim().equals("")) {
            stdouterrfile = null;
        }
        final String startserver = "--startserver:portfile=" + portfile + ",logfile=" + logfile + ",stdouterrfile=" + stdouterrfile + ",poolsize=" + poolsize + ",keepalive="+ keepalive;

        if (background.equals("true")) {
            sjavac += "%20" + startserver;
            sjavac = sjavac.replaceAll("%20", " ");
            sjavac = sjavac.replaceAll("%2C", ",");
            // If the java/sh/cmd launcher fails the failure will be captured by stdouterr because of the redirection here.
            String[] cmd = {"/bin/sh", "-c", sjavac + " >> " + stdouterrfile + " 2>&1"};
            if (!(new File("/bin/sh")).canExecute()) {
                ArrayList<String> wincmd = new ArrayList<String>();
                wincmd.add("cmd");
                wincmd.add("/c");
                wincmd.add("start");
                wincmd.add("cmd");
                wincmd.add("/c");
                wincmd.add(sjavac + " >> " + stdouterrfile + " 2>&1");
                cmd = wincmd.toArray(new String[wincmd.size()]);
            }
            Process pp = null;
            try {
                pp = Runtime.getRuntime().exec(cmd);
            } catch (Exception e) {
                e.printStackTrace(err);
                e.printStackTrace(new PrintWriter(stdouterrfile));
            }
            StringBuilder rs = new StringBuilder();
            for (String s : cmd) {
                rs.append(s + " ");
            }
            return rs.toString();
        }

        // Do not spawn a background server, instead run it within the same JVM.
        Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    JavacServer.startServer(startserver, err);
                } catch (Throwable t) {
                    t.printStackTrace(err);
                }
            }
        };
        t.start();
        return "";
    }
private static String fork ( String sjavac , String portfile , String logfile , int poolsize , int keepalive , final PrintStream err , String stdouterrfile , String background ) throws IOException , ProblemException { if ( stdouterrfile != null && stdouterrfile . trim ( ) . equals ( "" ) ) { stdouterrfile = null ; } final String startserver = "--startserver:portfile=" + portfile + ",logfile=" + logfile + ",stdouterrfile=" + stdouterrfile + ",poolsize=" + poolsize + ",keepalive=" + keepalive ; if ( background . equals ( "true" ) ) { sjavac += "%20" + startserver ; sjavac = sjavac . replaceAll ( "%20" , " " ) ; sjavac = sjavac . replaceAll ( "%2C" , "," ) ; String [ ] cmd = { "/bin/sh" , "-c" , sjavac + " >> " + stdouterrfile + " 2>&1" } ; if ( ! ( new File ( "/bin/sh" ) ) . canExecute ( ) ) { ArrayList < String > wincmd = new ArrayList < String > ( ) ; wincmd . add ( "cmd" ) ; wincmd . add ( "/c" ) ; wincmd . add ( "start" ) ; wincmd . add ( "cmd" ) ; wincmd . add ( "/c" ) ; wincmd . add ( sjavac + " >> " + stdouterrfile + " 2>&1" ) ; cmd = wincmd . toArray ( new String [ wincmd . size ( ) ] ) ; } Process pp = null ; try { pp = Runtime . getRuntime ( ) . exec ( cmd ) ; } catch ( Exception e ) { e . printStackTrace ( err ) ; e . printStackTrace ( new PrintWriter ( stdouterrfile ) ) ; } StringBuilder rs = new StringBuilder ( ) ; for ( String s : cmd ) { rs . append ( s + " " ) ; } return rs . toString ( ) ; } Thread t = new Thread ( ) { @ Override public void run ( ) { try { JavacServer . startServer ( startserver , err ) ; } catch ( Throwable t ) { t . printStackTrace ( err ) ; } } } ; t . start ( ) ; return "" ; }
Fork a background process. Returns the command line used that can be printed if something failed.