This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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 :-)
Joined: Apr 20, 2010
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com