• 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:

java Zoo did not work

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just started to learn Java with the book from Jeanne Boyarsky. On my PC running Windows 7, the command "javac Zoo.java" worked, the "Zoo.class" file has been created. But the command "java Zoo" sent the error message "Could not find or load main class Zoo". the PATH variable has been set to the JDK. Help most welcome

Patrick
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Can you please show us the code you compiled, the exact commands you used to compile and run the code, and tell us what working directory you ran the commands from?
 
P Menage
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Stephan,

Thank you for your reply.

My code is :

public class Zoo
{

public static void main(String[] args)
{
System.out.print ("Welcome to java");
}

}

I am working from directory c:\Java\OCA

The commands issued are:

Javac Zoo.java which worked. Java Zoo, which did not work.

Patrick
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange. If C:\Java\OCA is the folder where the .class file appears after compilation, and it's the current directory when you run the java command, it should work as long as Zoo.java file doesn't contain a package statement.

From the directory where the .class file appears, can you run javap -p Zoo, and post the results here? That's javap with a p. Make sure you enclose the results within code tags like [code]this[/code], when you post on this board. You should also do the same when you post code.
 
P Menage
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Stephan,

I battled with it, I do not know what I did, now it works.

I would prefer to have a clear solution, but at least I may continue my training.

Thank you for your help.

Patrick
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you didn't save the source file properly after you edited it? Anyway, I'm glad that it works. Good luck with your Java adventure.
 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check whether class path is set to the current directory (where Zoo.class created)
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you mean the CLASSPATH environment variable, that's a bad idea. Don't use it, it will lead to surprising results.

If you're compiling source files and the current working directory is not the root package, use the -sourcepath switch to supply the directory of the source files explicitly. If during compilation or at runtime the current working directory is not the root package, use the -cp switch to supply the directory of the class files explicitly. Do NOT use the CLASSPATH variable.

In this case, neither the -sourcepath nor the -cp switches are necessary, because the current working directory is the root package where both the sources and the compiled classes are located.
reply
    Bookmark Topic Watch Topic
  • New Topic