0

I'm trying to run a simple file that just prints "Hello World", on my desktop- working on a Mac OS. But I keep getting this error: Error: Could not find or load main class my filename.java

I've looked at other questions on stack overflow and they seem to suggest things about the class path, which I followed but to no avail- I'm just extremely confused on how to remedy this.

Siri
  • 1
  • What command are you using? – Zilvinas Sep 06 '17 at 19:55
  • First of all, you need to compile your class before you can run it. Second, you need to use the right command to run it. For example, `java second` and not `java second.java`. – Jesper Sep 06 '17 at 20:04
  • I compiled it before running. Once I do javac second.java, and look in my directory- a class file isn't generated. I get the error after I do 'java second' – Siri Sep 06 '17 at 20:06
  • @Zilvinas it works when I do 'java -cp . Solution', Solution being the name of my class, but how do I get it to work without having to type all that? – Siri Sep 06 '17 at 20:12
  • @Siri - as @Jesper already mentioned, first you need to compile your class using `javac second.java`, and then you can run it using `java second.class`. Keep in mind the naming conventions, that file name has to match the class name... ( `Solution.java` -> `Solution.class` ) – Zilvinas Sep 06 '17 at 20:16
  • @Zilvinas wow... that was a super dumb mistake! Thanks for the help guys- if it's not obvious already I'm a complete beginner to java haha. – Siri Sep 06 '17 at 20:20
  • If a class file is not generated after compiling, then it means something went wrong during compilation - carefully look at the error messages the compiler prints out. (Obviously, you cannot run a program if it did not compile without errors and there is no class file). – Jesper Sep 07 '17 at 06:11

0 Answers0