Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I took a program that someone wrote for a terminal-like shell interface for UNIX. However, I would like to change this so it can emulate a command prompt, not a terminal.

The program compiles, but if I try, for example, running the simple command of "dir" within the program then it panics and exits. Something similar to this shows on exit:

CreateProcess error=2. The system cannot find the file specified.

The only command that runs successfully is the "help" command (and granted, the "exit" and "" commands.)

I would appreciate any help that could be given regarding the program below:

Java
import java.io.*;
import java.util.*;

public class shell
{
    public static void main(String[] args) throws java.io.IOException {
        
        String commandLine;
        BufferedReader console = new BufferedReader
            (new InputStreamReader(System.in));
        
        while (true) {
            // read what the user entered
            System.out.print("My shell>");
            commandLine = console.readLine(); {
                // if the user entered a return, just loop again
                if (commandLine.equals("")) {
                    continue;
                }
                else if (commandLine.equalsIgnoreCase("exit")) {
                    System.out.println("Goodbye");
                    System.exit(0);
                }
                
                // split the string into a string array
                
                ArrayList<String> parms = new ArrayList<String>();
                String[] lineSplit = commandLine.split(" ");
                
                int size = lineSplit.length;
                for (int i=0; i<size; i++) {
                    parms.add(lineSplit[i]);
                }
                
                ProcessBuilder pb = new ProcessBuilder(parms);
                Process proc = pb.start();
                
                // obtain the input stream
                InputStream is = proc.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                
                // read what is returned by the command
                String line;
                while ((line = br.readLine()) != null)
                    System.out.println(line);
                    
                br.close();
                
            }
        }
    }
}
Posted

1 solution

Commands such as "dir" are internal to the command shell, and not freestanding programs. So you have to send them to a command shell process, you cannot invoke them in the way you have in your code. For this simple program you could just try sending every command to the shell in that way. The program name would be cmd.exe and the parameters would be a concatenation of "/C ", the actual command name, and the parameters to that command.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900