• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Compile error: Could not find or load main class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saved HelloWorld.java on my desktop (made in Notepad)
here is my PATH environment variable: C:\Program Files\Java\jdk1.8.0_151\bin
here is my cmd line C:\Users\Mike\Desktop>javac HelloWorld.java
the return is: Error: Could not find or load main class HelloWorld.java

What am i doing wrong? I've tried a few different iterations of both path and the cmd line and where i place the HelloWorld file -- all to no avail.

windows 7pro 64-bit.
Humble thanks
 
author & internet detective
Posts: 42134
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
Welcome to CodeRanch!

What's inside HelloWorld.java?
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, try listing out the files in your Desktop folder, make sure you have HelloWorld.java and not HelloWorld.java.txt
 
Marshal
Posts: 80616
468
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I can't see how you can get that error from that particular command line instruction. If you didn't have the HelloWorld.java file in that folder, you would get something like this:-The $ represents the prompt. The only way you can get “main class not found” is if you used the java command. There are several possible explanations.
  • 1: The code failed to compile, but you would have error messages from the javac tool.
  • 2: The name of the file is wrong. Be careful about the difference between HelloWorld and Helloworld.
  • 3: If you name a class without a main method you can also get that same error message.
  • 4: The file is there but you are trying to open the wrong file.
  • I think no 4 is what is happening here. What you are supposed to do is this:-Note you don't write .java or anything for the second instruction. It takes the name of the class, not the name of a file.

    You haven't shown a compile time error. What you are showing is the JVM starting, the runtime being unable to complete the task you have given it, and throwing an exception. Remember all exceptions occur at runtime. This is what happened when I tried it with JDK9u1 and no hello world applications anywhere near:-See that the last line of the error message tells you there is an Exception, and the Error object mentioned in the first line also counts as a sort of exception.

    I don't think it is a good idea to keep all your work in the desktop folder. It will become so full youi will never see the desktop picture again Look here for what I usually recommend. Move all your Java® work into your new directory. Stop using NotePad. Look here for what I recommend (Notepad++) and which options I think you should use.
     
    Mike Palomo
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Awesome. Success and some good info. Thank you. I feel I crossed Montana on foot, in the winter and I made it to the other side where there might be some coffee and a warm spot with slippers waiting for me. I moved the file from the desktop to the C:\ drive, changed my cmd,  followed the java <name>.java call then java <name> and all good. Onward and upward
     
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You aren't looking in the correct location in your command prompt. You have to navigate over to the save location using the command "cd". then run the "javac" command.
     
    Campbell Ritchie
    Marshal
    Posts: 80616
    468
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the pie
     
    Sheriff
    Posts: 7126
    185
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mickey Loveless wrote:You aren't looking in the correct location in your command prompt. You have to navigate over to the save location using the command "cd". then run the "javac" command.


    Not entirely true.  If you had a file c:\foo\bar\bar.java and you were at a command prompt cd'd to c:\, you could issue the command

    c:\>javac \foo\bar\bas.java
     
    reply
      Bookmark Topic Watch Topic
    • New Topic