• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running a .class file

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am just learning Java and ran into something that made me curious. It started as a funny error that kept popping up every time I tried to run something I just compiled. I would type "java program.class" and get a funny class not found error. I looked up ther error, checked my class path and kept having problems. I eventually discovered that the issue was the ".class". So I typed "java program" and it all worked beautifully. Now, I wonder why.

Why can I not run "java program.class"?

Thanks!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the "java" command takes as parameter a class name (which doesn't include the trailing ".class"), not a file name. Saves you from typing those extra characters :-)
 
Charles Nodell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense. You tell it to run the class "program". Java then searches your classpath for a class called program and finds it in the program.class file. Does that mean that when I entered "java program.class" it was searching for a class named program.class and would have run a file names program.class.class if it found it in the classpath? Can a class have a period in the name. I don't think so, but I am not sure.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does that mean that when I entered "java program.class" it was searching for a class named program.class and would have run a file names program.class.class if it found it in the classpath? Can a class have a period in the name. I don't think so, but I am not sure.


No, class names can't contain dots or most other special characters; google for "JLS" (the Java Language Specification) if you're interested in the gory details.

"java program.class" would actually look for a class called "class" that's part of a package called "program" - the dot serves as separator between package names (and between package name and class name). Those details can be found in the Java Tutorial: http://java.sun.com/docs/books/tutorial/java/package/index.html (which you should bookmark if you haven't done so yet, it's a good introductory source for all kinds of things Java).
 
Charles Nodell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thanks for the explanation and the link! I do not really want the gory details and I doubt I would understand them at this point anyways, but I feel that your answers have given me the information I need.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic