0

I am taking a Udemy class and I am stuck on the first lesson, getting command prompt to write "Hello world" through java. My error is once I have compiled it, and it creates the class file, and I try to run it through "java HelloWorld" it doesn't run. The error is.

Error: Could not find or load main class HelloWorld

And I can't figure it out, Here is a screenshot showing everything I have: https://app.box.com/s/4heybbazxswm4otjazrw

I have looked through my class discussion and no one had the error, and no StackOverflow topics seemed to be the same problem.

ben75
  • 29,217
  • 10
  • 88
  • 134
CalebK
  • 353
  • 2
  • 11
  • 3
    http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean – libik Jul 19 '14 at 21:49
  • 1
    What is the first line of your .java file? – McLovin Jul 19 '14 at 21:50
  • My First Line is : "public class HelloWorld" – CalebK Jul 19 '14 at 21:51
  • Are you in the same directory as the class file? – McLovin Jul 19 '14 at 21:51
  • 2
    `java -cp . HelloWorld` – Elliott Frisch Jul 19 '14 at 21:52
  • @ElliottFrisch IT worked!!!!! YAYAYAY now can you explain to me abit of why? What is this "-cp ." telling my computer? – CalebK Jul 19 '14 at 21:55
  • 1
    The `-cp` option specifies the classpath, which is the directory that Java looks in to find class files. Usually its default value is `.` (the current directory), but apparently not in this case. – McLovin Jul 19 '14 at 21:58
  • @Cubby208 - wondering why you are using udemy. Most of those courses are junk. Try using text books instead if you can't find alternate sources for good video tutorials. Here are some suggestions of various levels of textbook - head first java, java - how to program by deitel, thinking in java. Good luck and use google to get clues for errors in your code. – Erran Morad Jul 19 '14 at 22:01
  • 2
    The official Java tutorials (http://docs.oracle.com/javase/tutorial/getStarted/index.html) are really good. – McLovin Jul 19 '14 at 22:04

1 Answers1

5

Your CLASSPATH isn't set (or isn't set correctly). From the Java tutorial,

The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes.

However, you can also specify a CLASSPATH to the java runtime with the -cp argument (also aliased to -classpath. A colon separated list of folders, and jar/zip files to search for class files.

That's why

java -cp . HelloWorld

Allowed the JRE to find HelloWorld.class. You could also set CLASSPATH.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I did follow that part of the tutorial.... Something must be wacky their. Currently for the ClassPath variable I have written. Well originally i changed PATH to C:\Program Files\Java\jdk1.8.0_11\bin; however you are suggesting i change CLASSPATH so i made that one the same. I still need to use the -cp argument otherwise i get the error. – CalebK Jul 19 '14 at 22:12
  • What I am saying is you need your application to be in a folder specified in the CLASSPATH, or you need to use `-cp`. Or, you could use `-jar`. Or, you could try using an IDE. I suggest you read through that section of the tutorial again. – Elliott Frisch Jul 19 '14 at 22:15
  • Wait so the CLASSPATH variable needs to point to the file where i am doing all my java work? – CalebK Jul 19 '14 at 22:27
  • Directory, not file. And include, it's not necessarily exclusive. – Elliott Frisch Jul 19 '14 at 22:28
  • Ok just to make sure were on the right page i need some vocabulary help. Directory = basically a folder right? What does include and exclude mean in this sense? – CalebK Jul 19 '14 at 22:31
  • Yes. The variable may include many folders. Also jar files. And zip file paths. It is not an exclusive type of list that can only point exclusively at your program. – Elliott Frisch Jul 19 '14 at 22:34
  • Ok i cant seem to get it so that it can run without -cp is using -cp going to slow me down in the future? – CalebK Jul 19 '14 at 22:56