0

I'm new to Java coding, and I'm confused. I am using VS Code with the Java extension pack on a MacBook (not sure if this is relevant information or not). I wrote this:

package ICS111;

public class hello {
        public static void main(String[] args) {
            System.out.println("bruh moment");
        }
    }

To compile/execute, I used the command javac hello.java, which created a class called hello .class in the ICS111 folder. If I ls in my ICS11 folder, it outputs: (base) Kylies-MBP-3:ICS111 kylie$ ls hello.class hello.java

After this, I tried using java hello, but it gives me this error: Error: Could not find or load main class hello Caused by: java.lang.NoClassDefFoundError: ICS111/hello (wrong name: hello)

Any help would be greatly appreciated!

  • https://stackoverflow.com/a/18093929/11979793 this should answer your question – Hashir May 18 '20 at 21:44
  • Your class and filename should be same (case-sensitive), if they are not java will complain that way. – Ali Beyit May 18 '20 at 21:48
  • Navigate to your src folder using terminal. The first command should be to compile the java file. You can do this by typing the following `javac hello.java`. Then type the following to run `java hello` – Bradley May 18 '20 at 21:49
  • Does this answer your question? [How do I compile and run a program in Java on my Mac?](https://stackoverflow.com/questions/2360995/how-do-i-compile-and-run-a-program-in-java-on-my-mac) – Bradley May 18 '20 at 21:53
  • 1
    Your code is in a package. `cd..` then `java ICS111.hello` - the package name is part of the class. – Elliott Frisch May 18 '20 at 21:57
  • 1
    Thanks for all the links! I was in the wrong directory and I did not know I had to go one directory back. Thanks for the help! – Kylie U May 18 '20 at 21:59

0 Answers0