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.

9
  • 3
    stackoverflow.com/questions/18093928/… Commented Jul 19, 2014 at 21:49
  • 1
    What is the first line of your .java file? Commented Jul 19, 2014 at 21:50
  • 2
    java -cp . HelloWorld Commented Jul 19, 2014 at 21:52
  • 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. Commented Jul 19, 2014 at 21:58
  • 2
    The official Java tutorials (docs.oracle.com/javase/tutorial/getStarted/index.html) are really good. Commented Jul 19, 2014 at 22:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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.
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.
Wait so the CLASSPATH variable needs to point to the file where i am doing all my java work?
Directory, not file. And include, it's not necessarily exclusive.
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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.