Failed at Run a program

when I’m executing code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<String> inBuilts=  new ArrayList<>();
        inBuilts.add("type");
        inBuilts.add("exit");
        inBuilts.add("echo");


        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.print("$ ");
            String command = scanner.nextLine();

            String[] inputs =  command.split(" ");
            String commandInput = inputs[0];

            StringBuilder param = new StringBuilder();

            if(inputs.length > 2){
                for( int i=1;i<inputs.length;i++){
                    if(i<inputs.length-1){
                       param.append(inputs[i]).append(" ");
                    }else{
                        param.append(inputs[i]);
                    }
                }
            }else if(inputs.length > 1){
                param.append(inputs[1]);
            }


            if(commandInput.contains("exe")){
               commandInput = "YES";
            }

            switch (commandInput){
                case "type" :
                    if(inBuilts.contains(param.toString())){
                        System.out.println(param+" is a shell builtin");
                    }else {
                        String pathOutPut = handlepath(param.toString());
                        if(pathOutPut!=null){
                            System.out.println(param+" is "+pathOutPut);
                        }
                         else{
                            System.out.println(param + ": not found");
                        }
                    }

                    break;
                case "exit" :
                    if(param.toString().contains("0")){
                        System.exit(0);
                    }else{
                        System.out.println(commandInput+": command not found");
                    }
                    break;
                case "echo" :
                    System.out.println(param);
                    break;
                case "YES":
                    externalProgram(inputs[0],command);
                    break;
                default:
                    System.out.println(commandInput+": command not found");
            }
        }

    }

    public static String handlepath(String param){
        for(String path : System.getenv("PATH").split(":")){
            Path newPath = Path.of(path,param);                                        //The Path.of(path, parameter) method combines the directory path (path) with the parameter (which represents the command or file you are looking for) into a single Path object.
            if(Files.isRegularFile(newPath)){                                              //The Files.isRegularFile(fullPath) method checks if the fullPath represents a regular file (not a directory, symbolic link, etc.).
                return newPath.toString();
            }
        }
        return null;
    }

    public static void externalProgram(String pass, String input) throws IOException {
        String path = handlepath(pass);

        if (path == null) {

            System.out.printf("%s: command not found%n", pass);

        } else {

            String fullPath = path + input.substring(pass.length());

            Process p = Runtime.getRuntime().exec(fullPath.split(" "));

            p.getInputStream().transferTo(System.out);

        }
    }
}

getting below error

emote: [tester::#IP1] Running tests for Stage #IP1 (Run a program)
remote: [tester::#IP1] [setup] export PATH=/tmp/foo:$PATH
remote: [tester::#IP1] [setup] Available executables:
remote: [tester::#IP1] [setup] - custom_exe_9130
remote: [tester::#IP1] Running ./your_program.sh
remote: [your-program] $ custom_exe_9130 Emily
remote: [your-program] Program was passed 2 args (including program name).
remote: [your-program] Arg #0 (program name): /tmp/foo/custom_exe_9130
remote: [tester::#IP1] Output does not match expected value.
remote: [tester::#IP1] Expected: "Arg #0 (program name): custom_exe_9130"
remote: [tester::#IP1] Received: "Arg #0 (program name): /tmp/foo/custom_exe_9130"
remote: [your-program] Arg #1: Emily
remote: [your-program] Program Signature: 6343243234
remote: [your-program] $ 
remote: [tester::#IP1] Assertion failed.
remote: [tester::#IP1] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

how can i fix this , as i was not able to run it locally so cannot debug it

Thanks in advance

Hey @AgeOfUltra, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Thanks for replying but , I have solved that one now I have stuck at different points I will share you the GitHub Link

1 Like

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.