Reputation: 1
On running the code below I get an error could not find or load main class in java. It compiles without any error but on running it, I get an error on command line Could not find or load main class in java
import java.util.*;
import java.lang.*;
class p8{
public static void main(String args[]){
int[] ar = new int[10];
int i, result=0, temp;
for(i=0; i<args.length; i++){
ar[i] = Integer.parseInt(args[i]);
}
Scanner sc = new Scanner(System.in);
System.out.println("Enter your choice");
int Choice = sc.nextInt();
switch(Choice){
case 1: for(i=0; i<(ar.length); i++){
result = result + ar[i];
}
System.out.println("Sum is " + result);
break;
case 2: for(i=0; i<(ar.length); i++){
result = result + ar[i];
result = result/ar.length;
}
System.out.println("Average is " + result);
break;
default:
System.out.println("Invalid Option");
break;
}
}
}
Output:
C:\Program Files\Java\jdk1.7.0_25\bin>javac pr8.java
C:\Program Files\Java\jdk1.7.0_25\bin>java pr8 10 12 23
Error: Could not find or load main class pr8
Upvotes: 0
Views: 819
Reputation: 9437
To repeat the answer I provided in the comments:
Your classname (in code) must be identical to that of the .java file.
So, change this line:
class p8{
to
class pr8{
and try again.
For future reference, it's good practice to declare your classes as public:
public class p8{
would not have compiled, since the name of the class (in code) does not match the name of the file, while you can have only one public class for each java file, which must have the exact same name as the file.
ADDITION: Even though it is valid to have several classes in one file, the compiler will look for the main method in the class you specify, so if you run pr8, it will look for a main method in the pr8 class, not in another class in the same file.
ADDITION: Though I did not agree with the answer given by the other poster (who deleted his answer since then), if he had explained himself more, he would have had one point correctly:
Running java p8
would also have solved the problem
Upvotes: 3