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.
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.
Joined: Mar 22, 2005
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).
Joined: Apr 20, 2010
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.