• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file name

 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is said that the name of the program file shuld be d same as the
public class in d program if any and
if there is no public class then the program file can have any name.

but wen i tried the latter option it gives a runtime error y so??
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is said that the name of the program file shuld be d same as the
public class in d program if any and
if there is no public class then the program file can have any name.

but wen i tried the latter option it gives a runtime error y so??


Firstly UseRealWords
Your post is too unreadable so you wont get much response.

And what runtime error are you getting?
And take some time to read the Ask Good Question link below.

Hope this helps
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pdpta chopra:
but wen i tried the latter option it gives a runtime error y so??

Can you post the exact example and tell us step by step what you did? That would make it easier to explain why you got an error.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And instead of pdpta, why dont you use your real name.
pdpta does not sound real.Please see if you can change it.
See this for more information.
Thanks
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the above file is saved as diffrnt.java

it compiles,but when it is run it gives this runtime error:
Exception in thread "main"java.lang.NoClassDefFoundError: diffrrnt
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



the above file is saved as diffrnt.java

it compiles,but when it is run it gives this runtime error:
Exception in thread "main"java.lang.NoClassDefFoundError: diffrrnt
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try saving the file with testMoo.java.
It should solve the problem
But testMoo is not a really good convention for class name.
See this for more information.

Hope this helps
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes thanks for changing your display name.

Also take some time to read the Ask Good Question link below.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know saving the file as testMoo.java will solve the problem
but my question is why cant i save my program with any other name since my program does not have any public class??
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had no problem saving this with the name diffrnt.java

However, after using the command javac diffrnt.java this is what I got on my terminal.


As far as I know, in order to start a program you must start with the class that has main in it. Otherwise there's no entry point.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Nathan Leniz
i cannot read your output,its unreadable

my problem is that i get an exception message when i run diffrnt.java and not that i cannot save it or compile it

please see to it.
thankyou
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output is simply a listing of files in the directory after compiling diffrnt.java

Using the command javac diffrnt.java produces 3 class files, none of the with the name diffrnt.class.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i view the output if ther is no diffrnt .class file??
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main is your entry point.
[ July 21, 2008: Message edited by: Nathan Leniz ]
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that means the rule given in the book stating that if a source file name contains a public class,the filename must match the public class name goes for files that do not contain a main method

but if a file conatains a main metod it should be in a public class
and the source file should have same name.


is it how the book's statement means??
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure about rules, perhaps someone that knows more could help. If I were to make a program that had multiple classes, I'd probably put each class into its own file, with the filename the same as the class name (i.e Moo.java contains the Moo class blueprint). That would just make it easier for me to track down a class should I need to alter it later. You could make a file called Animals.java that contains the blueprints for many different animals, I just can't see a good reason why you would. Hopefully someone will clarify that.

Heck, you could put 20 different animal classes in Animals.java, and then put a MakeSound class with the main method in it. Then when you compile Animals.java you'll get 22 different classes, but your entry point would be MakeSound.

Of course if you're doing it to save typing when compiling multiple classes, you can always compile using something like

javac ./*.java
and that will compile all the .java files in the current directory (on my mac anyway, I would think it'd be the same on windows) or you can create a file like sourcefiles.txt and list the path to and the names of the files you want compiled.

Here is some good information on using javac.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well thanks nathan for your help

i hope some other fellow would clarify the exact cause
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is also relating to your other post, but if you have declared a class public, the file name must match the class name. You can only have one public class per file.

If you have non public classes, you can put them into a file named whatever you want. But again, as soon as you declare one of those classes public, the file name has to match that public class' name.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pradeepta
try this write a simple Java public class say HelloWorld which has a main method and simply prints Hello World ! and save it in a file with some other name say WorldHello.java.
Now try compiling this file and you should get compiler error.

Hope this helps
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you are using javac diffrnt.java to call the compiler, but you haven't told us what instruction you are using to call the java program.

In the example you have given, you should be calling it with java testMoo (not java testMoo.java, not java testMoo.class).
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was calling java diffrnt earlier
but calling java testMoo solved my problem.
thankyou
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, and thank you for changing the thread name to something more descriptive.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic