• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

a question about save

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class t1{
public static void main(String[] args) {
System.out.println("ok");
}
}
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?
thx
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a COMPLETE newbie with java but

class t1 {
public static void main(String[] args) {
System.out.println("ok");
}
}
Will have to be saved as tl.java

class t2 {
public static void main(String[] args) {
System.out.println("ok");
}
}
Will have to be saved as t2.java
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
macewan,
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.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic