This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes file name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "file name" Watch "file name" New topic
Author

file name

pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14280
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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

Joined: Jul 05, 2008
Posts: 137


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

Joined: Jul 05, 2008
Posts: 137



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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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

Joined: Jun 06, 2007
Posts: 2716
    
    6

And yes thanks for changing your display name.

Also take some time to read the Ask Good Question link below.
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
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

Joined: Nov 26, 2006
Posts: 132
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.


The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
@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

Joined: Nov 26, 2006
Posts: 132
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

Joined: Jul 05, 2008
Posts: 137
how can i view the output if ther is no diffrnt .class file??
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
main is your entry point.
[ July 21, 2008: Message edited by: Nathan Leniz ]
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
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

Joined: Nov 26, 2006
Posts: 132
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

Joined: Jul 05, 2008
Posts: 137
well thanks nathan for your help

i hope some other fellow would clarify the exact cause
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

@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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Jul 05, 2008
Posts: 137
i was calling java diffrnt earlier
but calling java testMoo solved my problem.
thankyou
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You're welcome, and thank you for changing the thread name to something more descriptive.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: file name