executing hadoop example jar files from java

2019-09-05 22:04发布

问题:

I am working with Hadoop 1.0.3 on Ubuntu 12.04, Eclipse 3.7 and am developing a GUI to execute Hadoop jobs using Windowbuider Pro. I need to execute the jar files containing my codes from the GUI. I have used the runtime.getruntime.exec() method with some success. However, while it does execute DFS-specific commands, it cannot execute .jar files, like the PiEstimator that comes bundled with the distribution. My code is as follows:

JButton btnYrhtrdsf = new JButton("Start Daemons");
btnYrhtrdsf.addMouseListener(new MouseAdapter() 
{
    @Override
public void mouseClicked(MouseEvent e) 
    {
    try { //WORKING
    Runtime.getRuntime().exec("/usr/local/hadoop/hadoop-1.0.3/bin/start-dfs.sh");
    Runtime.getRuntime().exec("/usr/local/hadoop/hadoop-1.0.3/bin/start-mapred.sh");
    } 
        catch (IOException e1) {
        e1.printStackTrace();
    }
    }
});
    contentPane.add(btnYrhtrdsf, "16, 10");

    JButton btnMakeDirectory = new JButton("Make Folder in HDFS");
    btnMakeDirectory.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {//WORKING
            try {
                Runtime.getRuntime().exec("/usr/local/hadoop/hadoop-1.0.3/bin/hadoop dfs -mkdir input2");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });
    contentPane.add(btnMakeDirectory, "12, 18");

    JButton btnStopDaemons = new JButton("Stop Daemons");
    btnStopDaemons.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try { //WORKING
                Runtime.getRuntime().exec("/usr/local/hadoop/hadoop-1.0.3/bin/stop-dfs.sh");
                Runtime.getRuntime().exec("/usr/local/hadoop/hadoop-1.0.3/bin/stop-mapred.sh");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });
    contentPane.add(btnStopDaemons, "16, 18");

    JButton btnPiestimator = new JButton("PiEstimator");
    btnPiestimator.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try { //PROBLEM!!!!!
                Runtime.getRuntime().exec("/usr/local/hadoop/hadoop-1.0.3/bin/hadoop jar hadoop-examples-1.0.3.jar pi 2 5");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });
    contentPane.add(btnPiestimator, "14, 26");
}

回答1:

Replace following line

Runtime.getRuntime().exec("/usr/local/hadoop/hadoop-1.0.3/bin/hadoop jar hadoop-examples-1.0.3.jar pi 2 5");

with:

Runtime.getRuntime().exec("/usr/local/hadoop/hadoop-1.0.3/bin/hadoop jar /usr/local/hadoop/hadoop-1.0.3/hadoop-examples-1.0.3.jar pi 2 5");