• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Impossible to find or load main class

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I've written the very first program every beginner do : HelloWorld

Here it is :
" public class HelloWorld
{
public static void main(String[] args)
{
System.out.println( "Hello World!" );
}
} "

I've then compiled it in the command with : javac HelloWorld.java
it worked but then when I try to run it with : java HelloWorld or java HelloWorld.class or java "c:\users\emmav\documents\csi\helloworld.class"
I get the error : impossible to find or load the main class

If anyone can hint me where I did wrong I'd be very thankful !
 
Rancher
Posts: 4739
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when I try to run it with : java HelloWorld


That should work if the HelloWorld.class file is in the current directory.

Please run the command and 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 so we can see exactly what was entered and the exact error message.

Also use the dir command to show what was in the current directory.
 
Emma Vande-Wouwer
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply, here is my command copy pasted :

"Microsoft Windows [Version 10.0.17134.228]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\emmav>dir "c:\users\emmav\documents\csi\"
Volume in drive C is OS
Volume Serial Number is 7E29-1FBC

Directory of c:\users\emmav\documents\csi

02/09/2018  13:38    <DIR>          .
02/09/2018  13:38    <DIR>          ..
02/09/2018  13:38               426 HelloWorld.class
02/09/2018  13:33               117 HelloWorld.java
              2 File(s)            543 bytes
              2 Dir(s)  57.900.068.864 bytes free

C:\Users\emmav>java HelloWorld
Error: Could not find or load main class HelloWorld

C:\Users\emmav> "
 
Norm Radder
Rancher
Posts: 4739
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you are not executing the java command in the directory holding the class file:


Directory of c:\users\emmav\documents\csi      <<<<<<<<< The directory with the class file

02/09/2018  13:38    <DIR>          .
02/09/2018  13:38    <DIR>          ..
02/09/2018  13:38               426 HelloWorld.class



The java command is issued in another directory:


C:\Users\emmav>java HelloWorld       <<<<<< Wrong directory.  The class file is in csi folder.



Try using the java command in the csi folder where the class file is located:


C:\users\emmav\documents\csi>java HelloWorld  

 
Emma Vande-Wouwer
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
awesome thank you !
 
Rancher
Posts: 644
10
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Emma,

When you post code, please use code tags.



 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emma Vande-Wouwer wrote:Thank you for your reply, here is my command copy pasted :

"Microsoft Windows [Version 10.0.17134.228]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\emmav>dir "c:\users\emmav\documents\csi\"
Volume in drive C is OS
Volume Serial Number is 7E29-1FBC

Directory of c:\users\emmav\documents\csi

02/09/2018  13:38    <DIR>          .
02/09/2018  13:38    <DIR>          ..
02/09/2018  13:38               426 HelloWorld.class
02/09/2018  13:33               117 HelloWorld.java
              2 File(s)            543 bytes
              2 Dir(s)  57.900.068.864 bytes free

C:\Users\emmav>java HelloWorld
Error: Could not find or load main class HelloWorld

C:\Users\emmav> "



Seems like your current directory is "C:\Users\emmav" and you are trying to execute in the directory "c:\users\emmav\documents\csi"  .
Please make sure to navigate to the proper path where your file exists and then execute the file.

and

java HelloWorld.class is a wrong way of executing a java file

instead you should use:
java HelloWorld
 
This will take every ounce of my mental strength! All for a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic