Ilonpilaaja
Ilonpilaaja

Reputation: 1249

Java process waitFor

I have the following method that converts a source code to html using gvim:

    String code2html( String s ) throws Exception {
    if ( s == null || s.length() == 0 ) return "";
    String txtfile = "dummyFile"+(UUID.randomUUID().toString())+".txt";
    String t;
    //PrintWriter w = new PrintWriter(Konst.FPATH+"tmp.txt","UTF-8");
    File fl = new File(Konst.FPATH+txtfile);
    FileWriter fw = new FileWriter(fl);
    PrintWriter w = new PrintWriter(fw);
    w.println(s);
    w.close();
    Runtime rt = Runtime.getRuntime();
    String T;
    try {
        // Process pr = rt.exec(T = "gvim -c \"set syntax=Java\" -c \"wq\" -c \"q\" "+Konst.FPATH+"tmp.txt");
        //Process pr = rt.exec(T = "gvim "+Konst.FPATH+"tmp.txt -s "+Konst.FPATH+"scrip.vim");
        Process pr = rt.exec(T = "gvim "+Konst.FPATH+txtfile+" -s "+Konst.FPATH+"scrip.vim");
        System.out.println(T);
        FileOutputStream fos;

        StreamGobbler errorGobbler = new StreamGobbler(pr.getErrorStream(),"ERROR");
        StreamGobbler outputGobbler = new StreamGobbler(pr.getInputStream(),"OUTPUT",fos=new FileOutputStream(Konst.FPATH+"garbage.out"));

        errorGobbler.start();
        outputGobbler.start();

        fos.flush();
        fos.close();
        int exitVal = pr.waitFor();

        StringBuilder sb = new StringBuilder();

        //BufferedReader b = new BufferedReader(new FileReader(Konst.FPATH + "tmp.txt.html"));
        if ( exitVal == 0 ) {
            BufferedReader b = new BufferedReader(new FileReader(Konst.FPATH + txtfile + ".html"));
            while ((t = b.readLine()) != null && !(t.length() >= 5 && t.substring(0, 5).equals("<body"))) ;
            do {
                sb.append(t + "\n");
            } while ((t = b.readLine()) != null && !(t.length() >= 6 && t.substring(0, 6).equals("</body")));
            sb.append(t);
            return sb.toString();
        }
        else return "";
    }
    catch ( Throwable e ) {
        e.printStackTrace();
    }
    return "";
}

The relevant StreamGobbler is here:

class StreamGobbler extends Thread {
InputStream is;
String type;
OutputStream os;

StreamGobbler( InputStream is, String type ) {
    this(is,type,null);
}
StreamGobbler( InputStream is, String type, OutputStream os ) {
    this.is = is; this.type = type; this.os = os;
}
@Override
public void run() {
    try {
        PrintWriter pw = null;
        if (os != null) {
            pw = new PrintWriter(os);
        }
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader r = new BufferedReader(isr);
        String line = null;
        while ((line = r.readLine()) != null) {
            if (pw != null)
                pw.print(line);
            System.out.println(type + ">" + line);
        }
        if (pw != null)
            pw.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

So, gvim is supposed to convert a source code to an html file called dummy+random string.html. And it does so. It is only that when it comes to BufferedReader to read the file, it does NOT find it ("No such file or directory"), although the file does appear in the relevant folder. So, obviously the file is created AFTER BufferedReader attempts to access is, despite the waitFor() command. How to fix this?

Upvotes: 0

Views: 285

Answers (1)

Sanjeev
Sanjeev

Reputation: 9946

Sometimes it take time to complete IO operations after finishing a process by Runtime#exec. so try putting some delay using Thread#sleep after Process#waitFor in order to let IO processing complete. And then try reading target file.

Hope this helps.

Upvotes: 2

Related Questions