Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error: Could not find or load main class BeerSong  RSS feed

 
Prasannakshi Grandhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I could not run the program BeerSong. Getting error "Error: Could not find or load main class BeerSong"

I have set the CLASSPATH and PATH correctly.

Code is



I have placed in the correct folder also as mentioned in the previous forums.

I am missing something here. Kindly help.

Thanks
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you trying to run this program? Using any IDE or command line?

If from command line, what commands are you using and from which folder are you running the command?
 
Prasannakshi Grandhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying using command line. I am new to Java programming and am not sure what I have missed. These are the details

CLASSPATH - C:\Program Files\Java\jre1.8.0_74\lib
PATH - C:\Program Files\Java\jdk1.8.0_74\bin

Command line Comments -
C:\Users\Prasannakshi>cd "C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01"

C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01>dir
Volume in drive C has no label.
Volume Serial Number is E23B-38D4

Directory of C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01

04/16/2016 11:33 PM <DIR> .
04/16/2016 11:33 PM <DIR> ..
04/16/2016 07:45 PM 6,148 .DS_Store
04/17/2016 12:00 AM 1,007 BeerSong.class
04/16/2016 07:45 PM 861 BeerSong.java
04/16/2016 11:38 PM 1,673 PhraseOMatic.class
04/16/2016 07:45 PM 1,307 PhraseOMatic.java
5 File(s) 10,996 bytes
2 Dir(s) 58,103,050,240 bytes free

C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01>javac BeerSong.java

C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01>java BeerSong
Error: Could not find or load main class BeerSong

C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01>


 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:
C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01>java chap01.BeerSong
 
Prasannakshi Grandhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same Error..


C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01>java chap01.BeerSong
Error: Could not find or load main class chap01.BeerSong

 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try removing from java code:
package chap01;
 
Prasannakshi Grandhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changed Code




Still getting the same error

C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01>javac BeerSong.java

C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01>java BeerSong
Error: Could not find or load main class BeerSong

C:\Users\Prasannakshi\Documents\JavaClass\hfjavafinalsamples\chap01>


I am moving to Eclipse now...I am not sure where i committed the mistake
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasannakshi Grandhe wrote:. . .
I am moving to Eclipse now...I am not sure where i committed the mistake
Don't use Eclipse at this stage. Tell us how you are compiling the .java files, whether you have the package declaration in the files, and what the name of the directory containing the .class file.

Your first post contains the mistake:-
You wrote:I have set the CLASSPATH and PATH correctly.
No, you haven't. You should uninstall that version of Java® and replace it with 8u77; unfortunately updates are quite frequent. Then both PATH and CLASSPATH are incorrect. The PATH is incorrect because it should include quite a lot more directories than that. Is that a system PATH or a user PATH? It might be all right as a user PATH.
The CLASSPATH doesn't point to anywhere you need to point to. You must have found an old book because setting the CLASSPATH like that has not been necessary for a long time. In fact, you should not set a system or user CLASSPATH at all. I think your problem will go away of you delete the CLASSPATH altogether. In the meantime, try the following at a command line:-
  • 1: echo %PATH%
  • 2: echo %CLASSPATH%
  • 3: set CLASSPATH=.
  • 4 (repeat): echo %CLASSPATH%
  • See whether the last makes any difference to your program.
     
    Carey Brown
    Bartender
    Posts: 2992
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So I tried this and it worked for me:

    cd \tmp\chap01

    [[comment out package line]]

    javac BeerSong.java

    java BeerSong

    [[output from program]]

    As a shot in the dark, remove your CLASSPATH entirely and see if it works. The only thing in my classpath are two extraneous jar files from 3rd party libraries that I downloaded. If you don't have any additional libraries then having no CLASSPATH should work with a correct installation of the Java SDK.
     
    Dave Tolls
    Rancher
    Posts: 2911
    35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't comment out the package line.
    That's Bad Practice.
    Your classes should (barring very odd exceptions that I can't even think of at the moment) be in packages.

    Once you've fixed the CLASSPATH (by getting rid of it entirely from the environment variables thing) you'll still have an issue, as you are trying to run the class from within its package.
    You should be executing it from the root of your classpath, which is the directory containing the chap01 directory.
    And the class is called chap01.BeerSong, so the command would be:

    java chap01.BeerSong
     
    Prasannakshi Grandhe
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, you haven't. You should uninstall that version of Java® and replace it with 8u77; unfortunately updates are quite frequent. Then both PATH and CLASSPATH are incorrect. The PATH is incorrect because it should include quite a lot more directories than that. Is that a system PATH or a user PATH? It might be all right as a user PATH.


    If i uninstall my Java..Is it gonna affect my programs in eclipse..PATH is a USER Path. Do you want me to remove the CLASSPATH? When i need to remove these commands after uninstalling and reinstalling with the new version of Java..? When i run these code where should i point my directory to..??

    1: echo %PATH%
    2: echo %CLASSPATH%
    3: set CLASSPATH=.
    4 (repeat): echo %CLASSPATH%


    Thanks for your time.
     
    Campbell Ritchie
    Marshal
    Posts: 55681
    162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Prasannakshi Grandhe wrote:. . . If i uninstall my Java..Is it gonna affect my programs in eclipse..
    No, but you may have to give Eclipse new locations for rt.jar and the documentation home.
    Project→Properties→Resources→Linked Resources→Edit path to rt.jar and the documentation.
    PATH is a USER Path.
    It is supposed to have things like C:\System.system32 in. Weren't those there beforehand?
    Do you want me to remove the CLASSPATH?
    Yes.
    When i need to remove these commands after uninstalling and reinstalling with the new version of Java..? . . .
    Edit the system PATH by changing ...1.8.0_74... to ..1.8.0_77...
    Delete the CLASSPATH. No other changes should be necessary. You usually need to edit your PATH when you install a new version of Java®.
    You navigate to the folder containing your code, or if you have a package name, to the directory “above” that directory so the first part of the package name is visible as a directory by writing DIR (Windows®) or ls (Linux).

    Thanks for your time.
    That's a pleasure
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!