Multiple classes in a single Java file, each with a main method - unexpected behavior?

末鹿安然 提交于 2019-12-19 09:07:11

问题


I have got the following code in a file called test.java which is located inside the directory C:\D\JavaProjects

class test
{
        public static void main( String[] str )
        {
                System.out.println( "Hello, World! from test" );
        }
}

class Test
{
        public static void main( String[] str )
        {
                System.out.println( "Hello, World!" );
        }
}

When I do "javac test.java" it outputs test.class. Now, if I do "java test" I get the following output:

Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: Test) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$000(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: test. Program will exit.

But when I do "java Test" I get

Hello, World!

Now, if I simply reverse the occurrence of the two class declarations, such that Test is declared BEFORE test, the java compiler outputs the file Test.class. Now doing "java test" gives the output:

Hello, World! from test

but "java Test" gives

Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: test) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$000(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: Test. Program will exit.

Now, I know it is very strange to have two classes with main in them in the same file, but this behavior seems completely illogical and more like a bug. Can somebody point me to the appropriate section of the Java Language Specification that specifies this behavior? Many thanks in advance for the help.


回答1:


Presumably you're running on Windows, right?

That means you can't have two classes which differ only in case - because they'll both end up wanting to be in the same file, as Test.class and test.class are effectively the same filename in case-insensitive file systems.

It's not really a bug in Java - just an unfortunate but natural corollary of using a case-insensitive file system in conjunction with a language which attaches meaning to filenames.




回答2:


@dark_secrets, You have to begin a class name with a capital letter in JAVA or else you will get an error while compiling.




回答3:


use public (then class name).... that will definetely work....



来源:https://stackoverflow.com/questions/5762897/multiple-classes-in-a-single-java-file-each-with-a-main-method-unexpected-beh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!