0

I am unable to call my java program from the command line. I keep getting this message:

"InitArray is not recognized as an internal or external command, operable program or batch file."

I can read the directory with the 'dir' command and see the program that I am trying to launch but cannot get past this error message!

Here is what I see: C:\Users\myName\Java_WorkSpace> And then I enter: InitArray 5 0 4

Again, I can see this program in the directory but I cannot access it. WHAT AM I DOING WRONG???

Here is the program code:

public class InitArray 
{
public static void main(String[] args)
{
    // check number of command-line arguments
    if ( args.length != 3)
        System.out.println(
    "Error: Please re-enter the entire command, including\n" +
    "an array size, initial value and increment.");
    else
    {
    int arrayLength = Integer.parseInt(args[0]); 
    int[] array = new int[arrayLength];

    int initialValue = Integer.parseInt(args[1]);
    int increment = Integer.parseInt(args[2]);

    // calculate value for each array element
    for ( int counter = 0; counter < array.length; counter++ )
        array[counter] = initialValue + increment * counter;

    System.out.printf("%s%8s\n", "Index", "Value");

    // display array index and value
    for ( int counter = 0; counter < array.length; counter++ )
        System.out.printf("%5d%8d\n", counter, array[counter]);
    } // end else
} // end main
} // end class InitArray

3 Answers 3

1

you need to type java first to call the Java executable.

Try this:

java InitArray 5 0 4

Sign up to request clarification or add additional context in comments.

Comments

1

Basically when using the Command line java you have to use - java.

For example you call your app from CMD using this -

java InitArray 5 0 4

If you bundle the files in a jar then the way of calling it is -

java -jar InitArray 5 0 4

1 Comment

@ Duli-chan,Thanks for taking the time to help. I hava placed 'java' before the call to 'InitArray' and now I am getting this error message: 'java' is not recognized as an internal or external command, operable program or batch file. This is what the command line looks like: C:\Users\Peter\Java_WorkSpace>java InitArray 5 0 4
0

I'm going to assume you are using Windows.

Here is how you fix your problem:

  • Get JDK from java. This link should get you to the download page. Install windows 32 or 64 bit whatever is appropriate for you system. Install it.
  • Ensure the jdk is in your PATH by running java -version in command line.
    • If you get "'java' is not recognized as an internal or external command, operable program or batch file" you need to add jdk to your path.
    • Navigate to C:/Program Files/Java/your_jdk/bin and look for java.exe. Right click java.exe, click on properties, and copy the filepath.
    • Open command prompt, run SET PATH="%PATH%;path_to_java"
      • To paste into command prompt, right click the icon in the top left, navigate to edit, then paste
    • Run java -version again and check to see that it is working.
  • Go to the directory with your java source file in windows explorer.
  • Hold shift, right click the whitespace by the contents of the folder, and click "open command window here".
  • Run javac "name_of_your_java_file.java"
  • Run java "same_name" 5 0 4

Happy java! Eclipse does all this for you by the way. Look into using an IDE.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.