Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with a waitFor() executing a script

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with a waitFor() executing a script

    The script works fine but stop when it ends...

    Here's the code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.lang.*;

    public class IOD extends JFrame implements ActionListener {

    File f;
    JLabel img;
    JScrollPane sc;

    private ImageIcon createImageIcon(String path) {
    try {

    return new ImageIcon(path);
    }
    catch (Exception e) {
    System.out.println(e.toString());
    return null;
    }
    }

    public void actionPerformed (ActionEvent ae) {
    JButton jb = (JButton) ae.getSource();

    if ( jb.getLabel().equals("Load image") ) { // Load Button
    try {
    JFileChooser fc = new JFileChooser();
    fc.showDialog(this, "Choose Image");
    f = fc.getSelectedFile();
    try {
    img.setVisible(false);
    sc.setVisible(false);
    }
    catch (Exception ex) {}
    img = new JLabel(createImageIcon(f.getAbsolutePath()));
    sc = new JScrollPane(img);
    this.getContentPane().add(sc, BorderLayout.CENTER);
    this.validate();
    }
    catch (Exception e) {}
    }

    if (jb.getLabel().equals("Predict orientation")){ // Predict Button
    try {
    runProcess(f);
    }
    catch (Exception e) {}
    }
    }
    private void runProcess(File f)throws Exception {
    this.f=f;
    File workDir = new File("c:/msys/1.0/bin");
    String[] cmdArray = new String[5];
    String image = new String (f.getName());
    cmdArray[0] = "c:/msys/1.0/bin/bash";
    cmdArray[1] = "--login";
    cmdArray[2] = "-i";
    cmdArray[3] = "-c";
    cmdArray[4] = "Extractor.sh " + image;
    Process p = Runtime.getRuntime().exec(cmdArray, null, workDir);
    StreamPumper out = new StreamPumper(new InputStreamReader(p.getInputStream()));
    StreamPumper err = new StreamPumper(new InputStreamReader(p.getErrorStream()));
    out.start();
    err.start();
    p.waitFor(); // here is the problem
    out.join();
    err.join();

    File workDir2 = new File("c:/tesi");
    String predictor = new String ("c:/tesi/predictor");
    Process predict = Runtime.getRuntime().exec(predictor, null, workDir2);
    StreamPumper out2 = new StreamPumper(new InputStreamReader(p.getInputStream()));
    StreamPumper err2 = new StreamPumper(new InputStreamReader(p.getErrorStream()));
    out2.start();
    err2.start();
    predict.waitFor();
    out2.join();
    err2.join();
    }

    class StreamPumper extends Thread {
    InputStreamReader input;
    StringBuffer buffer;

    StreamPumper(InputStreamReader input) {
    this.input = input;
    buffer = new StringBuffer();
    setDaemon(true);
    }

    public void run() {
    try {
    int c;
    while ((c = input.read()) != -1) {
    char ch = (char)c;
    buffer.append(ch);
    if (ch == '\n') {
    System.out.append(buffer.toString());
    buffer = new StringBuffer();
    }
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    public IOD() { // Frame
    super("Image Orientation Detector");
    JButton open = new JButton("Load image");
    JButton predict = new JButton("Predict orientation");
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(open, BorderLayout.NORTH);
    this.getContentPane().add(predict, BorderLayout.SOUTH);
    open.addActionListener(this);
    predict.addActionListener(this);
    this.setSize(300, 300);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    public static void main (String[] args) throws InterruptedException, IOException{
    IOD d = new IOD();
    }
    }
    Last edited by Baldurian; January 28th, 2010 at 08:51 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with a waitFor() executing a script

    The script works fine but stop when it ends...
    Shouldn't it stop when it ends? Please explain the problem a bit more

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with a waitFor() executing a script

    Quote Originally Posted by copeg View Post
    Shouldn't it stop when it ends? Please explain the problem a bit more
    Problem solved... I have completely removed the first waitFor() and keep the one on the second process. Now everything works fine.

Similar Threads

  1. [SOLVED] Detecting whether the piece of code is executing?
    By vivek1982 in forum Web Frameworks
    Replies: 4
    Last Post: March 27th, 2009, 06:17 AM