Here you can find the source of executeCD(String dirPath)
public static String executeCD(String dirPath)
//package com.java2s; //License from project: Open Source License import java.io.File; public class Main { public static final String ROOT = "user.dir"; public static String executeCD(String dirPath) { // TODO Auto-generated method stub String retStatus = null;/*from www . j a v a 2 s .c o m*/ try { if (dirPath.equals("..")) { File curr = new File(executePWD()); if (curr.getAbsoluteFile().getParent() != null) System.setProperty(ROOT, curr.getAbsoluteFile().getParent()); else System.out.println("::: Reached root directory :::" + ROOT); retStatus = "Directory changed successfully"; } else { File current = new File(System.getProperty(ROOT) + File.separator + dirPath); System.out.println("Inside CD ::" + current.getAbsolutePath()); if (current.exists() && current.isDirectory()) { System.setProperty(ROOT, current.getAbsoluteFile().getPath()); retStatus = ":::: Directory changed successfully ::::"; } else retStatus = "Directory does not exist. Enter correct directory name and try again."; } } catch (Exception e) { System.out.println("----Exception in CD command execution -----" + e.getMessage()); } return retStatus; } public static String executePWD() { // TODO Auto-generated method stub String currentDirectory = null; try { currentDirectory = System.getProperty(ROOT); } catch (Exception e) { System.out.println("----Exception in PWD command execution -----" + e.getMessage()); } return currentDirectory; } }