aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java source code File name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java source code File name" Watch "Java source code File name" New topic
Author

Java source code File name

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi everybody,
I have one question.
If there are no public classes or interfaces in our java file what should be the name of that file??
In Zaworski's site it is mentioned that the file name in that case should be different than the names of the classes and interfaces in our file.
sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
whichever class has main method should be the name of class .
------------------
"Winners don't do different things
They do things differently"


"Winners don't do different things<br /> They do things differently"
bhakti soman
Greenhorn

Joined: Nov 12, 2000
Posts: 22
hi
i have compiled this code
class bhatki
{
public static void main(String arg[])
{
}
}
saved it as demo.java works fine with me.
that means you need not save file same as that class which contains main mehod.
please help me if i am wrong.

Originally posted by sunilkumar ssuparasmul:
whichever class has main method should be the name of class .

meena sukumar
Greenhorn

Joined: Jan 11, 2001
Posts: 1
Hi ,
Java requires that the classname be the same as the name of the file.
Although ,your code had a class called Bhakti in a file named demo.java it would not give you any compile time error but at runtime u'll get the folllowing message
"Exception in thread "main" java.lang.NoClassDefFoundError:demo"
Meena
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi everybody,
I think that if you name the file different than all the classes or interfaces in that file it will compile fine but will give a runtime error : NoClassDefFoundError.

If you name the file with the name of the class that does not contain main method(interfaces cannot main method) then it will compile fine but will give a runtime error : NoSuchMethodError : main.
Am I true?
atin sehgal
Ranch Hand

Joined: Nov 12, 2000
Posts: 32
Hi!
There are a few things which i think I shd share with u first so that i am able to convince u.
1) The mofdifier.The modifier here is defaulty ansd not public.
2) Classs level whether top level or anyother..
If the modifier is public and class is top level its a necessary condition that for compiling that
a) Source Name shd be same as class name
For efficient running(no run time error_)
a) Main method shd b perfect.

Now the case of yr example..
Your class has no access modifir => it translates to friendly or default access.
Hence the condition for efficient compiling in this case is that
1) source file != name of class file.
they can be different.. and yet compile.
For running the condition ois :
That main method shd be correct..
Feel free to touchbase if any query.
I am attching a program that i have done today itself...

//top level class with default access modifier.Class compiles.It will not compile only when main method is not perfect
class abc /* this class will compile even if the file name is different from source file name*/
//class firstdefault//I saved the file earlier as firstdefault.java/.Then changed the name to a different source name.Still it compiled...
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

/* So conclusion...if a class file is having default access modifier , we can compile the class successfully
a) Even if source name different...from class name.
Runs successfully
a)only if main is absolutely correct.

Plz revert in case of doubt or anything wrong abt my code...
at atin_sehgal@hotmail.com

Originally posted by bhakti soman:
hi
i have compiled this code
class bhatki
{
public static void main(String arg[])
{
}
}
saved it as demo.java works fine with me.
that means you need not save file same as that class which contains main mehod.
please help me if i am wrong.

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi atin,
Even if the main method is absolutely correct and the file name is different than the class name still it will give runtime error : NoClassDefFoundError.
The main method has to be in the class which is the file name to run.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by meena sukumar:
Hi ,
Java requires that the classname be the same as the name of the file.
Although ,your code had a class called Bhakti in a file named demo.java it would not give you any compile time error but at runtime u'll get the folllowing message
"Exception in thread "main" java.lang.NoClassDefFoundError:demo"
Meena

The compiler will (should???) put the compiled class in Bhakti.class

42
bhakti soman
Greenhorn

Joined: Nov 12, 2000
Posts: 22
hi atin
i feel what code was written by me will compile fine and will not run even if main method is written properly
correct me if i am wrong

Originally posted by atin sehgal:
Hi!
There are a few things which i think I shd share with u first so that i am able to convince u.
1) The mofdifier.The modifier here is defaulty ansd not public.
2) Classs level whether top level or anyother..
If the modifier is public and class is top level its a necessary condition that for compiling that
a) Source Name shd be same as class name
For efficient running(no run time error_)
a) Main method shd b perfect.

Now the case of yr example..
Your class has no access modifir => it translates to friendly or default access.
Hence the condition for efficient compiling in this case is that
1) source file != name of class file.
they can be different.. and yet compile.
For running the condition ois :
That main method shd be correct..
Feel free to touchbase if any query.
I am attching a program that i have done today itself...

//top level class with default access modifier.Class compiles.It will not compile only when main method is not perfect
class abc /* this class will compile even if the file name is different from source file name*/
//class firstdefault//I saved the file earlier as firstdefault.java/.Then changed the name to a different source name.Still it compiled...
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

/* So conclusion...if a class file is having default access modifier , we can compile the class successfully
a) Even if source name different...from class name.
Runs successfully
a)only if main is absolutely correct.

Plz revert in case of doubt or anything wrong abt my code...
at atin_sehgal@hotmail.com


Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Guys,
The source file rules state:

  1. the file can contain only one public class or interface
  2. if the file contains a public class or interface the filename must be the same as the public class/interface and have the extension <code>.java</code>

  3. If a file has no public class, you can legally call it anything you want; it will compile fine. Think this is useful if you have a number of small utility classes in one file.
    However, if you have one class with <code>main()</code> defined, the filename must match the class name if you want to run application.

    This is wrong folks .. the name of the file does not have to match the name of the class having main() ... see post by ameen further on. .. Jane

    For example,
    If you save the following code in a file called anyname.java

    ... it will compile without an error. When you attempt to run <code>java anyname</code> you'll get the following error:

    Hope that helps.
    Jane
    [This message has been edited by Jane Griscti (edited January 13, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
atin sehgal
Ranch Hand

Joined: Nov 12, 2000
Posts: 32
Hi!
class abc /* this class will compile even if the file name is
different from source file name*/
//class firstdefault /8I saved the file earlier as firstdefault.java/.Then changed the name to a different source name.Still it compiled...*/
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Oops , I tried yr program , but it never ran , it compiled ok , but gave error at run time..
I tried the above program, in which I first compiled a class as firstdefault and saved it.It ran and compile...perfectly..
Then I renaed the class name as abc and saved as first default.java.
i compiled again , it compiled.
then it ran..
Thats why i quoted those things..
Plz suggest where I was wrong.i might be wrong too.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Atin,
If I understand you right:

  1. you created a class 'firstdefault' and saved it in a file 'firstdefault.java'
  2. you then compiled and ran the code without any problem
  3. you then renamed the class 'abc.java'
  4. compiled and ran the program without any problem

  5. Did you delete the 'firstdefault.class' file before you tried to run the code after you'd changed the class name to 'abc'?
    The second compile would produce an 'abc.class', not a 'firstdefault.class' ... if you failed to delete the result of the first compile then the original class is executed; not the one resulting from the second compile.
    Hope that helps.
    Jane
ameen ahamed
Greenhorn

Joined: Jan 02, 2001
Posts: 25
Hi amit,
there is no compulsion that you should give filename as same as the class name that holds the main function. It is for beginners till understatnd the process.
Basic function while you compile. Suppose your are having 3 classes in your demo.java file as follows
1. a1
2. a2
3. a3 (holds main function)
compile this file
javac demo.java
it will generate 3 class files called a1.class,a2.class,a3.class
to run the program from demo.java you should give the a3 class filename that holds the main function.
java a3 (//note that the class file name here is case sensitive).
So it is not mandatory to give the source file name as class name that holds main function.
ameen
Originally posted by amit sanghai:

Hi everybody,
I have one question.
If there are no public classes or interfaces in our java file what should be the name of that file??
In Zaworski's site it is mentioned that the file name in that case should be different than the names of the classes and interfaces in our file.

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Thanks Ameen,
You really cleared my doubt. I earlier wrongly stated that file name should be same as the class name having the main method to run correctly. As u correctly pointed out, during compiling we can give any name and during running the name should be the name of the class having the main method.
What if there are two class files , one having the main method and the other being an applet. Can we do that???
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
ameen,
You are absolutely right! I totally overlooked the obvious
Jane
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
I have an answer and interesting sample code to test the answer here, if you are willing to have a look:
What are the rules to name a Java file without public class defined in it?
Tell me how do you think.
Thanks!
Roseanne
[This message has been edited by Roseanne Zhang (edited January 13, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Great example Roseanne. Thanks.
Jane
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java source code File name