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

Java file name

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In K&B book its given that the file name can be any name with no matches on the class name if the classes are not declared public but i tried one example the file gets compiled but at run time i am getting error java.lang.NoClassDefFoundError. I have attached the code here.

class Test
{
public static void main(String ar[])
{
System.out.println("Welcome to SCJP");
}
}

file name is Name.java. I dont know where i made mistake. Kindly rectify my mistake.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can name the file anything you want as long as there is no public top-level class in the source file. But the .class files that are created will match the names of the classes in the source file.

[ March 07, 2006: Message edited by: Keith Lynn ]
[ March 07, 2006: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vejaykrishna
You are compiling file Name.java and java compiler gerates the Test.class and Name.class files so when u run the java program using Name class it shows NosuchMethodError because it doesnot have main method and u r trying on Name class ,Try to run program using java Test
 
Vejaykrishna Venkatesan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks swarupa patil & Keith Lynn Now i got it.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swarupa,

"You are compiling file Name.java and java compiler gerates the Test.class and Name.class files so when u run the java program using Name class it shows NosuchMethodError because it doesnot have main method and u r trying on Name class ,Try to run program using java Test"

Are you sure? If we compile file Name.java and java compiler generates the
"Test.class and Name.class" for the above code.
According to me it generates only one class file with name Test.class.

Correct me if i am wrong

Thanks a lot
 
sreedhar lak
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swarupa,
Are U sure ?I think it will creates only one class i.e Test.class.
Thanks a lot
 
Vejaykrishna Venkatesan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear sreedhar,

I think u r bit confused. Try compiling the prg it creates Name.class and Test.class.
 
swarupa patil
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sreedhar
You are correct it creates only one class
but if give both class defination of Name and Test
then only it genrates two classes .
 
Vejaykrishna Venkatesan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sreedhar,

U r correct. i made mistake.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Vejaykrishna"

Please click the My Profile link above to meet JavaRanch's naming policy of using your real first and real last names, in which there is a space between the two names too.

Thanks

Mark
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya it gives just one class file Test.class but can u tele me how to run this one. it also gives NoClassDefFoundError if v try to run tht class file.can u please tell me how to solve this error now.
 
Vejaykrishna Venkatesan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
"Vejaykrishna"

Please click the My Profile link above to meet JavaRanch's naming policy of using your real first and real last names, in which there is a space between the two names too.

Thanks

Mark



Thanks Mr. Mark. I dont know where to go and see the naming rules. But, vejaykrishna is one name and venkatesan is my last name.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the top of this forum under the Post a Reply or Post new message buttons there is a My Profile link, click that and under the field where it says display name, just add a space to the end of what you currently have and add your last name.

Thanks

Mark
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic