File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Name Of file !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Name Of file !!!" Watch "Name Of file !!!" New topic
Author

Name Of file !!!

jay nair
Greenhorn

Joined: Sep 13, 2005
Posts: 28





I am confused about file names , in the above code , if i keep the class name as nstack.java , the compiler says that class stack is public should be declared in a file name called stack.java ,

now if i give the file name as stack.java then it compiles just fine but but when i try 2 run it it gives error " Exception in thread " man" java.langNoSuchMethodError: main


also i wanted to know what does this statement mean ...

* Files with no public classes have no naming restrictions.

if the above is true then



will the above code work if the File name is bool.java ???( i have noticed that when i compile it it gives out bool1.class which is normal , now if thats the case isnt the statemnet ( * files with no public classes have no naming restrictions) contradicted ?


Help will be appreciated ...
thanks in advance


Preparing for SCJP 5
jay nair
Greenhorn

Joined: Sep 13, 2005
Posts: 28
I am confused about file names , in the above code , if i keep the class name as nstack.java , the compiler says that class stack is public should be declared in a file name called stack.java ,



wat i meant was if i keep the FILE NAME as nstack.java not class name ...

sorry
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I'm not sure what you're confused about. I can tell you that there are three unrelated things going on here.

1) When you compile any Java source file (a *.java file is called a "source file"), a class file is created for each class in the file. Each class file is named X.class, where X is the name of a class as given in the source file.

2) There's a rule that there can be only one public class in a source file, and if there is a public class in a source file, the source file must have the same name as that class, plus ".java".

3) When you run a Java application, the argument to "java" is the name of a class which contains a "main()" method: this name has nothing to do with the name of any source file, but only to do with the name of a class.


[Jess in Action][AskingGoodQuestions]
jay nair
Greenhorn

Joined: Sep 13, 2005
Posts: 28
thanks ernest ,

i guess i was not descriptive about my problem....

well in the stack program , there is one public class , so according to rule the name of the file must be the name of that class , now if i do that it compiles fine but since the class in which the main method is there has the name nstack so wen i try to Run the program it gives me error saying it did not find any MAIN method ,

so my question is , i s there anyway this program will run ... or the only possible way is to make another file and put the public class ( stack class) in that file and name the file as stack.java ....

hope i was clear ,
thanks for ur reply ,
would really appreciate further clarifications ,
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

As I tried to point out, all of these things are really unrelated. You tell the compiler (javac.exe) the name of the source file, and it creates some number of class files. Then you tell the runtime (java.exe) the name of the class you want to run, the one with main() in it. So in your case, you'd say

javac stack.java
java nstack
jay nair
Greenhorn

Joined: Sep 13, 2005
Posts: 28
thanks ernest ,
that makes sense , i guess i was seeing it in a complex way ....


thanks again ,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Name Of file !!!