Run cd shell command - Java Native OS

Java examples for Native OS:Shell Command

Description

Run cd shell command

Demo Code


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;

public class Main{
    private static final String[] WIN_RUNTIME = { "cmd.exe", "/C" };
    private static final String[] OS_LINUX_RUNTIME = { "/bin/bash", "-l",
            "-c" };
    public static void cd(String path) {
        String[] cmd = new String[1];
        cmd[0] = "cd '" + path + "'";

        if (isWin()) {
            path = path.replaceAll("\\\\/", "\\");
            cmd[0] = "cd /d \"" + path + "\"";
        }//from w ww . j a  v a2 s  .  c om
        List<String> rtn = SyncUtils.runProcess(cmd);
        checkErrors(rtn);

    }
    public static boolean isWin() {
        boolean isWin = false;
        String osName = System.getProperty("os.name");
        if (osName.contains("Windows"))
            isWin = true;
        return isWin;
    }
    public static List<String> runProcess(String... command) {
        return runProcess(false, command);
    }
    public static List<String> runProcess(boolean flgDebug,
            String... command) {
        File f = null;
        return runProcess(f, flgDebug, command);
    }
    public static List<String> runProcess(String workingPath,
            boolean flgDebug, String... command) {
        File f = new File(workingPath);
        return runProcess(f, flgDebug, command);
    }
    public static List<String> runProcess(File workingDir,
            boolean flgDebug, String... command) {
        boolean isWin = isWin();
        if (flgDebug) {
            System.out.print("command to run: ");
            for (String s : command) {
                System.out.print(s);
            }
            System.out.print("\n");
        }
        String[] allCommand = null;
        try {
            if (isWin) {
                allCommand = concat(WIN_RUNTIME, command);
            } else {
                allCommand = concat(OS_LINUX_RUNTIME, command);
            }
            ProcessBuilder pb = new ProcessBuilder(allCommand);
            pb.redirectErrorStream(true);
            if (workingDir != null)
                pb.directory(workingDir);
            Process p = pb.start();
            p.waitFor();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            String _temp = null;
            List<String> line = new ArrayList<String>();
            while ((_temp = in.readLine()) != null) {
                if (flgDebug)
                    System.out.println("temp line: " + _temp);
                line.add(_temp);
            }
            if (flgDebug)
                System.out.println("result after command: " + line);
            return line;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void checkErrors(List<String> rtn) {
        //Check the return value for erros
        if (rtn == null)
            return;
        try {
            rtn.get(0);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return;
        }
        String msg1 = rtn.get(0);
        if (msg1.matches("(?i)^(error|fatal).*$")) {
            System.out.println(rtn.get(0));
            System.exit(1);
        }
    }
    private static <T> T[] concat(T[] first, T[] second) {
        T[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }
}

Related Tutorials