'Why am I having the following errors compiling my java command line app? [closed]

I have this program:

package com.example.java

public class Main{
    
    public static void main(String[] args){
        
        for(i=0; i<args.length; i++){
            System.out.println("This is index " + i + "==> " + args[i]);
        }
    }
}

I have saved the java file in the directory:

C:\Users\tonny\clsandbox>

The first time I have compiled it with the command line:

PS C:\Users\tonny\clsandbox> javac Main.java

And landed to this error:

Main.java:1: error: ';' expected
package com.example.java
                        ^
1 error

Then thinking I am resolving that, I used this other command line instead:

PS C:\Users\tonny\clsandbox> javac com.example.java.Main.java

And landed into this other error:

error: file not found: com.example.java.Main.java
Usage: javac <options> <source files>
use --help for a list of possible options

Pleas help me out. I am joining the Java community.

Thank you in advance!



Solution 1:[1]

When we add the ; at the end of the first line we get an error i is not declared so we add int in the for loop.

package com.example.java;

public class Main{

    public static void main(String[] args){

        for(int i=0; i<args.length; i++){
            System.out.println("This is index " + i + "==> " + args[i]);
        }
    }
}

We can now compile.

Solution 2:[2]

correct written like this below package com.example.java; you lost symbol that ; at end of package statement.

Solution 3:[3]

package com.example.java;

public class Main{

public static void main(String[] args){
    
    for(int i=0; i<args.length; i++){
        System.out.println("This is index " + i + "==> " + args[i]);
    }
}

}

now it work remember dont forgot Semicolon

Solution 4:[4]

The program wants to be in a folder structure that agrees with the package statement. So Main.java wants to be in a folder like .../foo/com/example/java/Main.java

From folder foo/ use: javac com/example/java/Main.java

To run from folder foo/ use: java -cp . com.example.java.Main

There will be a Main.class file in com/example/java/, but do not include the ".class" in the name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 crDro
Solution 3
Solution 4