Results 1 to 9 of 9
  1. #1
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Compiles fine but error when try to run

    Hi All,
    Does someone have any idea why the following code works in Eclipse with no problems but on
    command line()does not run.

    Java Code:
    public class Hello{
    	public static void main(String args[]){
    
    	System.out.println("Hi");
    }	
    
    }
    it compiles fine and creates a class files as well, but gives the error:
    could not find or load main class Hello

    when i try to run this with:
    java Hello

    Thank You in advance!

    Tariq

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Compiles fine but error when try to run

    Where are you running this from?
    That is, what directoy, and does that directory contain the Hello.class file?
    Do you have a CLASSPATH environment variable set, if so then either remove it or use '-cp.' in your java command.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Compiles fine but error when try to run

    when you open the command prompt window enter: dir
    to show the folders contents before entering the java command.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Compiles fine but error when try to run

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Tariq>cd Desktop

    C:\Users\Tariq\Desktop>dir
    Volume in drive C has no label.
    Volume Serial Number is 9EAA-975F

    Directory of C:\Users\Tariq\Desktop

    14/05/2012 23:44 <DIR> .
    14/05/2012 23:44 <DIR> ..
    30/04/2012 23:42 1,126 Aptana Studio 3.lnk
    12/02/2012 18:21 870 Downloads - Shortcut.lnk
    19/04/2012 22:58 1,227 eclipse - Shortcut (2).lnk
    25/02/2012 13:02 949 eclipse - Shortcut.lnk
    12/05/2012 09:09 42 first.js
    30/04/2012 23:16 2,397 Google Chrome.lnk
    15/05/2012 16:50 406 Hello.class
    14/05/2012 23:44 100 Hello.java
    11/03/2012 19:26 1,282 Launch WhiteSmoke - Shortcut.lnk
    10/03/2012 11:05 2,394 MySQL 5.5 Command Line Client.lnk
    13/05/2012 13:39 <DIR> nodeWork
    21/04/2012 12:41 732 Photos Family - Shortcut.lnk
    12/05/2012 03:58 <DIR> Removed From Desktop Just after Second Semes
    ter
    14/05/2012 23:22 518 Test1.class
    14/05/2012 23:22 275 Test1.java
    12/05/2012 10:10 <DIR> wep tec week 12
    17/11/2011 19:47 696 Workspace - Shortcut.lnk
    14 File(s) 13,014 bytes
    5 Dir(s) 73,063,886,848 bytes free

    C:\Users\Tariq\Desktop>

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Compiles fine but error when try to run

    Where did you enter the java command and get the error message?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Compiles fine but error when try to run

    Oh sorry here it is now at the very end:

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Tariq>cd Desktop

    C:\Users\Tariq\Desktop>dir
    Volume in drive C has no label.
    Volume Serial Number is 9EAA-975F

    Directory of C:\Users\Tariq\Desktop

    14/05/2012 23:44 <DIR> .
    14/05/2012 23:44 <DIR> ..
    30/04/2012 23:42 1,126 Aptana Studio 3.lnk
    12/02/2012 18:21 870 Downloads - Shortcut.lnk
    19/04/2012 22:58 1,227 eclipse - Shortcut (2).lnk
    25/02/2012 13:02 949 eclipse - Shortcut.lnk
    12/05/2012 09:09 42 first.js
    30/04/2012 23:16 2,397 Google Chrome.lnk
    15/05/2012 16:50 406 Hello.class
    14/05/2012 23:44 100 Hello.java
    11/03/2012 19:26 1,282 Launch WhiteSmoke - Shortcut.lnk
    10/03/2012 11:05 2,394 MySQL 5.5 Command Line Client.lnk
    13/05/2012 13:39 <DIR> nodeWork
    21/04/2012 12:41 732 Photos Family - Shortcut.lnk
    12/05/2012 03:58 <DIR> Removed From Desktop Just after Second Semes
    ter
    14/05/2012 23:22 518 Test1.class
    14/05/2012 23:22 275 Test1.java
    12/05/2012 10:10 <DIR> wep tec week 12
    17/11/2011 19:47 696 Workspace - Shortcut.lnk
    14 File(s) 13,014 bytes
    5 Dir(s) 73,063,886,848 bytes free

    C:\Users\Tariq\Desktop>java Hello
    Error: Could not find or load main class Hello

    C:\Users\Tariq\Desktop>javac Hello.java

    C:\Users\Tariq\Desktop>java Hello
    Error: Could not find or load main class Hello

    C:\Users\Tariq\Desktop>

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Compiles fine but error when try to run

    Looks like the java command should find the class file or give a different error message. I can not get the error message you get. Here's what I get:


    D:\JavaDevelopment\Testing\ForumQuestions7>dir Hello*
    Volume in drive D is Work
    Volume Serial Number is 7CE8-AEC3

    Directory of D:\JavaDevelopment\Testing\ForumQuestions7

    05/15/2012 04:15 PM 406 Hello.class
    05/15/2012 04:15 PM 114 Hello.java
    2 File(s) 520 bytes
    0 Dir(s) 56,083,308,544 bytes free

    D:\JavaDevelopment\Testing\ForumQuestions7>java Hello
    Hi

    D:\JavaDevelopment\Testing\ForumQuestions7>
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Compiles fine but error when try to run

    What's the output from java -version?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Compiles fine but error when try to run

    *sigh*
    Is my post invisible?

    You have CLASSPATH set in your environment variables.
    That is the list of places 'java' will look for the Hello class if you do not provide a '-cp .' to the java command.
    Either remove the CLASSPATH variable or use the -cp switch.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 07-01-2011, 03:20 AM
  2. Replies: 6
    Last Post: 02-08-2011, 02:29 AM
  3. Code runs fine but get compile error
    By BillyB in forum New To Java
    Replies: 4
    Last Post: 11-29-2010, 02:18 PM
  4. Compiles in Netbeans, error in textpad
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 04:50 AM
  5. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •