概述
I have got the following code in a file called test.java which is located inside the directory C:DJavaProjects
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.
解决方案
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.
最后
以上就是发嗲黑夜为你收集整理的一个java文件有几个主类,单个Java文件中的多个类,每个类都有一个主方法 - 意外行为?...的全部内容,希望文章能够帮你解决一个java文件有几个主类,单个Java文件中的多个类,每个类都有一个主方法 - 意外行为?...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复