File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes a question about save Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a question about save" Watch "a question about save" New topic

a question about save

po-ting Yeh

Joined: Nov 22, 2003
Posts: 1
class t1{
public static void main(String[] args) {
if I save this to file t1,
then compile and runtime are ok,
but after doing this step,
if I just save it to another file t2 without changing class name,
then compile t2 is ok,
but producing runtime exception,
please tell me why?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Becuase the fully qualified (package) name of the class must match the relative path (from the CLASSPATH) of the file that contains the class. In your case the package is the default (or root) so the compiled class file must be located at ./t1.class from any of the paths defined in the CLASSPATH. That is how the bootstrap ClassLoader locates the class files that are necessary to run the application. If you had placed the class in the com.mypackage package then it would need to be located at com/mypackage/t1.class for the ClassLoader to find it.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Natraj Gudla
Ranch Hand

Joined: Feb 09, 2003
Posts: 34
When you say "java t2" to execute the java code, the class file needs to contain a class by name "t2" with the the proper main method declared.
As you have renamed the file name , but did not change the class name, it gives a RuntimeException.
Hope this explanation helps.
Robert MacEwan

Joined: Apr 05, 2003
Posts: 13
I'm a COMPLETE newbie with java but

class t1 {
public static void main(String[] args) {
Will have to be saved as

class t2 {
public static void main(String[] args) {
Will have to be saved as

Robert MacEwan
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. A single part display name doesn't cut it.
Thanks Pardner! Hope to see you 'round the Ranch!
Note that it's public outer classes that must be in a file with the same name as the class. If the classes aren't declared to be public, then they can be in just about any ol' source file - no matter the file name.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: a question about save
It's not a secret anymore!