0

I have a program titled "Averages.Java" that runs fine in my IDE but when I navigate to the proper location for the program in command prompt and attempt to run it, I'm given the error message

"Error: Could not find or load main class Averages Caused by: java.lang.ClassNotFoundException: Averages

Here is the code for the program

import java.util.Scanner;

public class Averages1 {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("Enter first user name: ");
    String firstName = in.next();

    System.out.print("Input first number: ");
    int num1 = in.nextInt();

    System.out.print("Enter second user name: ");
    String secondName = in.next();

    System.out.print("Input second number: ");
    int num2 = in.nextInt();

    System.out.print("Enter third user name: ");
    String thirdName = in.next();

    System.out.print("Input third number: ");
    int num3 = in.nextInt();

    System.out.println(firstName +": "+ num1);
    System.out.println(secondName +": "+num2);
    System.out.println(thirdName +": "+num3);
    System.out.println("Total of 3 numbers is : " + (num1 + num2 + num3));
    System.out.println("Average of 3 numbers is: " + (num1 + num2 + num3) / 3);
  }
}
Beri
  • 11,470
  • 4
  • 35
  • 57

3 Answers3

2

You should save your file as Averages1.java, as that's what your class name is.

Then, navigate to where that file is, and use this command:

javac Averages1.java

Now, there will be a Averages1.class file generated. To run it, you need to use this command:

java Averages1
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • It tells me that javac is not recognized as an internal or external command,operable program or batch file. Am I missing some software? –  Sep 16 '18 at 20:22
  • @skell012 You need to download javac if you don't have it installed already. If you have it already and you are still getting this error, you need to set the PATH environment variable to include the folder containing javac. – Sweeper Sep 16 '18 at 20:37
  • I figured out how to use javac and it created a class file as you said. I tried using java Averages 1 and it gave me yet another error message"C:\Users\Sean\Documents\NetBeansProjects\Averages\src\averages>java Averages1 Error: Could not find or load main class Averages1 Caused by: java.lang.NoClassDefFoundError: averages/Averages1 (wrong name: Averages1)" –  Sep 16 '18 at 20:42
  • @skell012 did you save the file as “Averages1.java” like I said in the first paragraph? – Sweeper Sep 16 '18 at 21:02
  • Yes the files name is Averages1.java –  Sep 16 '18 at 21:17
  • Is it possible I need to specify the package? I've made sure the file is Averages1.java and I have the class file in the same folder as the java file so I'm not sure what else to do. –  Sep 16 '18 at 22:16
0

Simply put, your class names must match the .java filename they're contained in.

Since your class is defined as Averages1, it must be saved in a file names Averages1.java.

The Java compiler will always throw this error if the names do not match.

Most modern IDEs should have already warned you about this while you were creating your class (IntelliJ IDEA does, for example).

Zephyr
  • 9,885
  • 4
  • 28
  • 63
0

To the unlucky guys having the same problem: Don't read this answer, go here.

Hey there, welcome to stackoverflow!

I've flagged this question as a duplicate of this question, the question has an extensive answer which should help you quite a lot (especially wink regarding packages).

In one comment you shared your java-command output, which raised the following exception:

Error: Could not find or load main class Averages1
Caused by: java.lang.NoClassDefFoundError: averages/Averages1 (wrong name: Averages1) 

Looking at the exception which caused the error, you can see that the java command is looking for the Averages1 class in the averages package. This means you didn't share your full source code. You probably have defined the package on top of your Averages1.java source file, looking like package averages;. When you use packages you need to use the fully qualified class names including the package name when using the java-command.

java averages.Averages1

Unfortunately the java-command expects the Averages1 class file (which you generated earlier using the javac-command) to be in the averages folder. To run Averages1 from the current working directory you need the Averages1.class file to be located in the averages subdirectory:

/ <-- current working directory
/averages/Averages1.class

So. You see, this whole bunch is kinda messy, doesn't scale when you have multiple files with multiple classes (if you really want to do it manually without scripts you madman!) and if third party libraries (fancy math stuff, logging, etc) is involved which will be the case 100% at some point, you don't want to do this manually. Luckily there are people who make your life easier. Build tools like gradle or Maven deal with all this stuff, leaving you with just a file where you just define what you want (in your case probably a jar file, more or less a runnable zip file) and what libraries you want to include. Same goes for IDE's like Eclipse and IDEA. If you really want to do this stuff manually, make sure that you read the manual of the commands you are using.

sn42
  • 2,353
  • 1
  • 15
  • 27
  • Thanks, I saw the similiar question you referred me to and while it is helpful I'd like to ask you how to make the class file go in the proper subdirectory and if this is the final step before it is able to run properly. –  Sep 16 '18 at 22:40
  • This answer should address this issue: https://stackoverflow.com/a/11032623/8089107 – sn42 Sep 16 '18 at 22:43