Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error: Could not find or load main class  RSS feed

 
Dustin Wright
Ranch Hand
Posts: 30
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting an error trying to run a welcome app from a book. I've looked at the FAQs and can't seem to solve the issue. I'm guessing my path and/or classpath are not right. Here's my path environment variable.

C:\jdk1.8.0_20\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Windows\Microsoft.NET\Framework64\v4.0.30319

Here's my output.

09/01/2014 10:23 PM 634 Welcome.class
09/01/2014 10:21 PM 422 Welcome.java
14 File(s) 126,243,703 bytes
9 Dir(s) 53,942,079,488 bytes free

C:\jdk1.8.0_20>set CLASSPATH=java welcome

C:\jdk1.8.0_20>java welcome
Error: Could not find or load main class welcome

C:\jdk1.8.0_20>set CLASSPATH=

C:\jdk1.8.0_20>java welcome
Error: Could not find or load main class welcome

C:\jdk1.8.0_20>

It looks like the version command gives me what it should, so it appears I've got java setup right, but I"m stuck on the error I'm getting
C:\jdk1.8.0_20>javac -version
javac 1.8.0_20

C:\jdk1.8.0_20>

Please help, thanks.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dustin,

Welcome to the Ranch!

I'm not sure about your class path, or if you have placed the code in a package that could cause an issue, but do you notice a problem with the snippet below:

09/01/2014 10:23 PM 634 Welcome.class
09/01/2014 10:21 PM 422 Welcome.java
C:\jdk1.8.0_20>java welcome

Hint: Remembering that Java is a case sensitive language, is there anything odd in "Welcome.class" and then calling that class with "java welcome"?
 
Dustin Wright
Ranch Hand
Posts: 30
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! Yes, I was doing it wrong

C:\jdk1.8.0_20>java Welcome
Welcome to Core Java
by Cay Horstmann
and Gary Cornell

C:\jdk1.8.0_20>
 
Dustin Wright
Ranch Hand
Posts: 30
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, now I've got the same issue with BeerSong.

09/01/2014 10:23 PM <DIR> .
09/01/2014 10:23 PM <DIR> ..
09/01/2014 09:59 PM 1,007 BeerSong.class
09/01/2014 09:57 PM 864 BeerSong.java

08/27/2014 03:47 PM <DIR> bin
07/30/2014 02:25 PM 3,244 COPYRIGHT
08/27/2014 03:47 PM <DIR> db
08/04/2014 04:33 PM <DIR> docs
08/27/2014 03:47 PM <DIR> include
08/27/2014 04:46 PM 9,786,038 javafx-8u20-apidocs.zip
08/27/2014 03:47 PM 5,025,527 javafx-src.zip
08/27/2014 04:26 PM 89,937,288 jdk-8u20-docs-all.zip
08/27/2014 03:47 PM <DIR> jre
08/27/2014 03:47 PM <DIR> lib
08/27/2014 03:47 PM 40 LICENSE
08/27/2014 03:47 PM 159 README.html
08/27/2014 03:47 PM 527 release
08/27/2014 04:10 PM <DIR> src
07/30/2014 02:25 PM 21,199,439 src.zip
08/27/2014 03:47 PM 110,114 THIRDPARTYLICENSEREADME-JAVAFX.txt
08/27/2014 03:47 PM 178,400 THIRDPARTYLICENSEREADME.txt
09/01/2014 10:23 PM 634 Welcome.class
09/01/2014 10:21 PM 422 Welcome.java
14 File(s) 126,243,703 bytes
9 Dir(s) 53,940,092,928 bytes free

C:\jdk1.8.0_20>java BeerSong
Error: Could not find or load main class BeerSong


Again, I'm sure I'm doing it wrong, just don't know what it is I'm doing wrong. Any help would be greatly appreciated. Thank you!!
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dustin,

It's really tough to know without seeing the code, but is your main method signature correct to be an entry point?
For example, this is a valid main method signature that the compiler has no problem with: That will compile fine. However, since I dropped off the static keyword, when I attempt to use that main method as an entry point to my program, the java interpreter won't be able to find it.

Also, are you using the default package, or a defined package? If you are using a defined package, then you need to make sure your folder structure matches to where the BeerSong.class file should be.
After checking those items, if you are still having difficulty, please post your code - please remember to wrap the code correctly so that it is readable (← Click here if you are not sure).
 
Dustin Wright
Ranch Hand
Posts: 30
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the sample code for the book Head First Java. I've not edited the code at all. In the book, it says it is not going to return the expected result, so I'm just trying to get this sample code to run. I've tried to run the compiled version provided and I've tried to compile the .java file. Same result. Here's the code, but again, it's un-edited sample code from the book. Thanks for the help!

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15985
86
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a package statement there:

The directory structure must match the package structure.

That means that you should put BeerSong.java and BeerSong.class in a directory named "chap01", and then start the program from the directory that contains the directory "chap01". You also need to specify the fully qualified name of the class, like this:

java chap01.BeerSong
 
Dustin Wright
Ranch Hand
Posts: 30
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, ok three questions come to mind

1) Is this mentioned in the book at all?
2) Can I remove that package reference to make it easier to work with?
3) Sorry, this is my first exposure to Java and have only had a few weeks of C#, very ignorant. How should this look?


A) Tried adding the "chap01" folder from the sample code in my jdk folder, and tried to call it, that did not work
C:\jdk1.8.0_20>cd c:\jdk1.8.0_20\chap01

c:\jdk1.8.0_20\chap01>java BeerSong
Error: Could not find or load main class BeerSong

B) Tried putting the "chap01" folder on the root of C and that did not work
c:\>java chap01.BeerSong
Error: Could not find or load main class chap01.BeerSong

How should I set this up?

I really appreciate your help. Please assume I know nothing more than what's in the book.
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dustin Wright wrote: . . . 1) Is this mentioned in the book at all?
Not sure. I don't think so. The code in the book, as far as I can remember, misses out the package names.

2) Can I remove that package reference to make it easier to work with?. . .
Yes. In fact I think you should simply remove the package name. Come back to packages when you have more practice. You can read about packages in the Java Tutorials.

You mentioned C#. C# does not use packages but it has things called namespaces which I think are similar.

I suggest:-
  • 1: Edit the .java file by deleting the line starting package or prefixing it with // which converts it to a comment.
  • 2: Save the .java file.
  • 3: Compile it with the instruction javac BeerSong.java
  • 4: Execute it with the instruction java BeerSong
  • 5: Tell us what happened.
  • Don't copy‑and‑paste this post. You will have to execute the app from the same directory the BeerSong files are in.

     
    Dustin Wright
    Ranch Hand
    Posts: 30
    1
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you! I copied the BeerSong.java file to here:

    C:\jdk1.8.00_2

    removed the package reference at the top and saved the file.

    I compiled it with:

    C:\jdk1.8.0_20>javac BeerSong.java

    That returned no errors and I see BeerSong.class in C:\jdk1.8.00_2 right above BeerSong.java

    Tried to run it and got the error.

    C:\jdk1.8.0_20>java BeerSong
    Error: Could not find or load main class BeerSong

    C:\jdk1.8.0_20>

    I am sure I'm not doing it right. I really appreciate your help.
     
    Paweł Baczyński
    Bartender
    Posts: 2026
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try
    java -cp . BeerSong

    If it works with -cp that means you set your CLASSPATH to somethig that does not include current directory.
    Either add . to CLASSPATH or remove CLASSPATH entirely.
     
    Dustin Wright
    Ranch Hand
    Posts: 30
    1
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paweł Baczyński wrote:Try
    java -cp . BeerSong

    If it works with -cp that means you set your CLASSPATH to somethig that does not include current directory.
    Either add . to CLASSPATH or remove CLASSPATH entirely.


    Thank you, C:\jdk1.8.0_20>java -cp . BeerSong did work. I'm sorry, I don't understand what you are trying to tell me about CLASSPATH. If you are referring to Enviornment Variables, I did add the following to the Path variable. I don't have one called CLASSPATH.

    C:\jdk1.8.0_20\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Windows\Microsoft.NET\Framework64\v4.0.30319


     
    Paweł Baczyński
    Bartender
    Posts: 2026
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dustin Wright wrote:If you are referring to Enviornment Variables (...)

    Yes, I was referring to a environment variable named CLASSPATH. I should have made that clear. Sorry.

    Dustin Wright wrote:I don't have one called CLASSPATH.

    Are you sure? You wrote in your first post that you executed:
    Dustin Wright wrote:C:\jdk1.8.0_20>set CLASSPATH=java welcome


    Type echo %CLASSPATH% and post the output here.
     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We're making progress

    Don't put your work in the C:\Java folder. If you uninstall Java and install a new version, you may find your work vanish altogether. I have some suggestions here.
     
    Dustin Wright
    Ranch Hand
    Posts: 30
    1
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I did, I read I read that in one of my books. I knew I messed it up. Sorry, I did not know. OK, here's the output you asked me to provide.

    C:\jdk1.8.0_20>echo %CLASSPATH%
    java BeerSong

    C:\jdk1.8.0_20>

    I don't know what that means, yet. How can I undo this?

    Regarding the work in that java directory, yes of course, I'll read your suggestions and fix that too.

    Thanks!
     
    Chris Barrett
    Bartender
    Posts: 321
    24
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Dustin,

    This link will help you reset the CLASSPATH to the location you want: http://www.coderanch.com/how-to/java/HowToSetTheClasspath
    Following those instructions and resetting the CLASSPATH should overwrite any incorrect CLASSPATH you might have accidentally set.
     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The CLASSPATH tells the JVM/javac tool where to find classes referred to in your app. If you don't set a CLASSPATH at all, which is probably the best default, it uses the . = “current directory”. You need a CLASSPATH when you start downloading .jar files, for example to work a database connection.
     
    Dustin Wright
    Ranch Hand
    Posts: 30
    1
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That seems to have done it. I created a new environment variable named ClassPath and set it to CLASSPATH = “current directory”. Here's what seems odd if I remember correctly. Things still did not work, so I went back and deleted that environment variable and now it works. I think what I did when I deleted it was reset it to a default configuration. So, with everyone's help, we fixed my screw-up. Thank you all!

    We'll see what I can break next!

    Thanks again.
     
    Paweł Baczyński
    Bartender
    Posts: 2026
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Suppose by current directory you mean C:\java
    So you set your ClassPath variable to CLASSPATH = C:\java
    What you should have done was setting CLASSPATH to C:\java.

    But, after all, deleting CLASSPATH entirely was the best solution.
    Do not mess with CLASSPATH until you know that you need to.
     
    Dustin Wright
    Ranch Hand
    Posts: 30
    1
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paweł Baczyński wrote:But, after all, deleting CLASSPATH entirely was the best solution.
    Do not mess with CLASSPATH until you know that you need to.


    Agreed and understood! Thank you VERY much!!
     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You never actually need to set a system classpath. You may need a classpath for specific apps, but it will be different for each, so you set individual classpaths. If a classpath has already been set (QuickTime does that, or at least used to), you may need to alter it by adding . to its beginning.
     
    Sarmad Alsaadi
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paweł Baczyński wrote:Try
    java -cp . BeerSong

    If it works with -cp that means you set your CLASSPATH to somethig that does not include current directory.
    Either add . to CLASSPATH or remove CLASSPATH entirely.


    Thank you so much. This has resolved the same issue that was discussed here.
     
    Campbell Ritchie
    Marshal
    Posts: 54886
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch
    This shows that old threads can be useful if the same problem recurs.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!