• 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

files with no public classes...

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

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

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right Alton, didn't read the question properly
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alton ...Great explanation.
--Cathy
reply
    Bookmark Topic Watch Topic
  • New Topic