0

I'm trying to learn Java for the first time and I'm following a toutorial but i keep getting the same error after i try to run my code :/ My code:


public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.print("I love pizza");
    }

}

My error:

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

I've already created a 'Main' class but it won't work.

I thought it would print 'I love pizza' to the console window like it did in the toutorial i was watching but that does not happen :/

User102
  • 1
  • 2
  • 6
    The key lies in **how** you are running it. Command Line? Which OS? In an IDE? – Stewart Jul 26 '23 at 22:17
  • Welcome to Stack Overflow. Please read [ask] and take the [tour]. It is especially important to note that this is **not a discussion forum**, and posts you make here are not the start of "threads"; they are expected to be **questions** that can be concretely answered. Please do not use the answer section to follow up unless you are answering your own question, and please do not talk about yourself in the question. – Karl Knechtel Jul 26 '23 at 23:09

2 Answers2

0

For me, it works …

I stored the code above to a file named Main.java in an empty folder, than I started a console, moved to that folder and entered

% java Main.java

That returned I love pizza as expected.

I was using Java 17; if you use an older version, you may have to type

% javac Main.java
% java Main
tquadrat
  • 3,033
  • 1
  • 16
  • 29
0

Make sure you have "classname" and "filename" are same. It's case-sensity.

//filename: Main.java

public class Main{
 public static void main(String args[]){  
 System.out.println("Hello Java");  
 }
}
//Hope this helps.