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.

Programs written in Java programming language need a main() method to be run/executed because it is where the program execution begins. Occasionally when you run a Java program, you might see the error “Could not find or load main class” . You get this error because you are incorrectly trying to run the main() inside the class using java command. For example, If your source code name is HelloWorld.java, your compiled code will be HelloWorld.class. You will get that error if you call it using:

java HelloWorld.class

Instead, use this:

java HelloWorld

Other reasons to occur this error is :

  • File Extension
  • Wrong package
  • Invalid Classpath
  • Wrong Class Name