files with no public classes...
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
This is supposed to display "Hello World".
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
--Cathy
| A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









