• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java source code File name

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whichever class has main method should be the name of class .
------------------
"Winners don't do different things
They do things differently"
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
bhakti soman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
atin sehgal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ameen,
You are absolutely right! I totally overlooked the obvious
Jane
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great example Roseanne. Thanks.
Jane
 
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic