Correct code not running from bash

I’m currently working on the FizzBuzz exercise (https://www.codecademy.com/courses/learn-java/projects/java-fizzbuzz). I wrote my code in an IDE and there it gives zero errors and runs perfectly, with the expected output.
Yet when I paste the exact code in the online exercise, save the code and then try to run it from the bash (with the command java FizzBuzz), I get an error: Error: Could not find or load main class FizzBuzz
Caused by: java.lang.ClassNotFoundException: FizzBuzz.

How do I get the exercise to run?

  • Did you save your filename as FizzBuzz.java?
  • try running ls command to list the files in your directory… is the file in your directory?

I didn’t change any of the filenames in the online environment for the exercise, so the name is still FizzBuzz.java. If I run ls, it lists fizzbuzz.class and FizzBuzz.java.

To me, that seems like it should allow me to run it, but I still get the same error. (for context, I had the same problem with other exercises, like the language families one, but in those I wasn’t 100% sure I had not made any coding errors. Here I the thing runs perfectly in my IDE)

Can you share your code? Maybe someone can try to replicate the error.

Oh… Given how often a browser is the problem, that could definitely be the source. Hadn’t even thought of that yet.
Here is my code, I’d love to know if I need to start bughunting in my browser or if there is something else going on.

class fizzbuzz {

	public static void main(String[] args) {
		
		for (int i = 1; i <= 100; i++) {
			if (i % 3 == 0 && i % 5 == 0) {
				System.out.println("Fizzbuzz");
			}
			else if (i % 5 == 0) {
				System.out.println("Buzz");
			}
			else if (i % 3 == 0) {
				System.out.println("Fizz");
			}
			else {
			System.out.println(i); }
		}
	}
}

Run the command java fizzbuzz.java first to compile. Then you can run java fizzbuzz afterwards on the compiled file. Note my casing is matching the class name lower casing. I think it’s good for it to match (so filename FizzBuzz.java will mean the class should be named FizzBuzz, but it should run either way as long as the case matches the filename).

1 Like

Wow. Somehow, it works now. I changed the capitalisation of my class, compiled it (still with the same error, strangely enough) and then it ran. Yay! Thanks for the help!

The naming convention tip is a good one to remember, I’ve had issues with that before.

Yea the naming convention thing I want to say is some sort of standard but don’t quote me on that, I haven’t written java in a while…

1 Like

Probably. I have done some python a while ago and am now getting into java to prepare for a job course I’m starting in a week. I get the concepts, but get a bit fuzzy on the details :sweat_smile:

I seem to recall, though I cannot immediately put my finger on the appropriate section of the docs or another resource to confirm my recollection, that Java source is case sensitive.

In which case, if your class name was fizzbuzz but the exercise expected FizzBuzz this would be the error. It would also explain why it was resolved when you changed the name of your class.

1 Like

You’re probably right. Given how Java is usually very case sensitive, I think both of you pointed out the problem. I made the mess myself… I’ll just file this under ‘sometimes you learn best from making mistakes’!

1 Like