Error: Could not find or load main class

So about halfway through working a project I start getting the above error, I naturally start commenting things out that I just added with no avail. javac returns nothing. It starts happening to every single other exercise I start doing. I’m not sure what causes this, I report it as a bug and it starts working again after a few days and re occurs within a few hours again.

I’m currently on the Best Fare Calculator, I just started it and it’s returning the above error, here’s my code for now:

import java.util.Arrays;

class TransitCalculator {

public Integer numberOfDays;
public Integer numberOfRides;

public TransitCalculator(Integer days, Integer rides) {
numberOfDays = days;
numberOfRides = rides;
}

Double Fare = {2.75,33.00,127.00};

public double unlimited7Price(Integer days, Integer rides) {
double pricePerRide = Fare[1] / ( days * rides );
return pricePerRide;
}

public void main(String args){

System.out.println(unlimited7Price(19,20));

}
}

( apologies for the bug detective tag I couldn’t find a tag for best fare calculator )

You could start with a minimal program (Hello World), run and to see if that’s better, and then either start comparing or or if it doesn’t run then that’s still information.

You may want to start by running the hello world program in your own controlled environment. If you can’t get that to run in a controlled environment, then you know to look up a tutorial for that.

(Get into the habit of making sure that your base program runs before you start writing other things, and you may similarly want to run after each change you make since that is the base to the next change you’re making)

that doesn’t seem to do anything either :confused:

Certainly did something, because that’s not the same error message. It’s complaining about something. What? Do you agree with what it is complaining about, should it be the way it says, do you think you’ve made that happen?

Here’s what you need to do.

Create a file. Later you will tell the compiler (javac) about this file.
Put a class in the file.
Put several, if you want. In the same file, or several.
One of those classes needs to have a static main method in it.
Compile all files you created.
Tell java which class to execute. That’s a class that has a static main method.

While executing, when JVM does not find a .class file with the specified name then a run time error occurs saying “Could not found or load main class”. The reason why this happens is mostly due to:

  • Wrong Class Name
  • Invalid Classpath

Main class could not be found when there is typo or wrong syntax in the fully qualified java class name or it does not exist in the provided classpath. You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder, add . to your classpath. Note that the Windows classpath separator is a semi-colon ; . If you want to execute the main() method in MainClass, you must use the full class name, including package name, in the java command.