wood burning stoves*
The moose likes Beginning Java and the fly likes Running a .class file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running a .class file" Watch "Running a .class file" New topic
Author

Running a .class file

Charles Nodell
Greenhorn

Joined: Apr 20, 2010
Posts: 6
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!


Charles
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41800
    
  62
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 :-)


Ping & DNS - my free Android networking tools app
Charles Nodell
Greenhorn

Joined: Apr 20, 2010
Posts: 6
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
Marshal

Joined: Mar 22, 2005
Posts: 41800
    
  62
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

Joined: Apr 20, 2010
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running a .class file