Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes files with no public classes... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "files with no public classes..." Watch "files with no public classes..." New topic
Author

files with no public classes...

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi,
I dont quite understnad this concept:
Files with no public classes. have no naming restrictions....
I created a class as follows, and named it Test.java.
class TestFileName
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
The program compiles fine, but crashes during runtime
java.lang.NoClassDefFoundError: TestFile
Exception in thread "main"
Can someone please explain this concept?
Thanks,
Cathy.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Cathy Song:
Hi,
I dont quite understnad this concept:
Files with no public classes. have no naming restrictions....
I created a class as follows, and named it Test.java.
...

the compiler does not complain if you have a java file with no public classes. But, since you are using the main method and trying to run the java file, your java file must have the same name as your java class.

File SubTest.java

it compiles but throws a:
Exception in thread "main" java.lang.NoSuchMethodError: main
Now:
File SubTest.java

Compiles and prints Hello world.
Hope this helps


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Or another peculiar case when you can still run a class "without" a main() method is inheritance. This is Andres' code with minor modifications:

This is supposed to display "Hello World".
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Cathy Song:
Hi,
I dont quite understnad this concept:
Files with no public classes. have no naming restrictions....
I created a class as follows, and named it Test.java.
class TestFileName
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
The program compiles fine, but crashes during runtime
java.lang.NoClassDefFoundError: TestFile
Exception in thread "main"
Can someone please explain this concept?
Thanks,
Cathy.

Hi Cathy,
It really doesn't matter if the name of your source file is different from your class name. The only restriction is that if you define a public class then that is the only time that your source file should be the same as your class name.
As for your sample code, it will work fine. I believe that the reason you got the error is because you run it like this:
java Test
This will fail because the program cannot find Test.class. When you compile a program, it outputs a class file based on the class name, i.e. it will output TestFileName.class. Therefore, in you working directory, you will end up with two files, namely:

Test.java
TestFileName.class


Now, if you want to execute that program, you should instead run it like this:
java TestFileName
Hope this helps.
[ September 01, 2003: Message edited by: Alton Hernandez ]
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
You're right Alton, didn't read the question properly
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Thanks Alton ...Great explanation.
--Cathy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: files with no public classes...
 
Similar Threads
multiple classes
public class
Compiling Question
Can a main method be inherited?
regarding public classes