java 如果我写了 system.out.println() ,则找不到包系统;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6921594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
package system not found in case I wrote system.out.println();
提问by Mohammad Faisal
i'm writing a simple program:
我正在写一个简单的程序:
class Demo{
public static void main(String[] args){
system.out.println("Hello");
}
}
on compilation it gives an error: package system not found. Why it do so that package not found instead system is a misspelled class name.
编译时出现错误:未找到包系统。为什么这样做是因为找不到包而不是系统是拼写错误的类名。
回答by Nivas
Why it do so that package not found instead system is a misspelled class name
为什么这样做是因为找不到包而不是系统是拼写错误的类名
When you say something.somethingElse
the compiler assumes you are doing a packageName.classname
. In this case you were intending to access out
of the System
class, but you could very well be trying to access a package called system
that is not present (in the classpath for instance). So it is a guess from a compiler.
当您说something.somethingElse
编译器假定您正在执行packageName.classname
. 在这种情况下,你打算访问out
的的System
类,但你很可能试图访问一个叫包system
中不存在(在类路径实例)。所以这是编译器的猜测。
And (I guess) it is so because this is the betterguess. Let's say the compiler said class not found. You might be happy. But tons of others doing Java.util.List
(instead of java.util.List
) would complain "I was trying to access the java.util
package but misspelled it. The compiler wrongly says Missing class name.
而且(我猜)是这样,因为这是更好的猜测。假设编译器说class not found。你可能会很高兴。但是很多其他人Java.util.List
(而不是java.util.List
)会抱怨“我试图访问该java.util
包但拼错了它。编译器错误地说Missing class name。
Update (for the sake of completeness)
From @paxdiablo's answerbelow:
更新(为了完整起见)
来自@paxdiablo 的回答如下:
The reason why the compiler is assuming it's a package name rather than a class or variable name lies in section 6.5 of the JLS, "Determining the meaning of a name"
编译器假设它是包名而不是类或变量名的原因在于 JLS 的第 6.5 节“确定名称的含义”
回答by Jesus Ramos
System.out.println("Hello");
You need a capital S
你需要一个大写的 S
Package names are lower case like java.lang.SomeClass, since it's lowercase it assumes you're looking for a package named system.
包名是小写的,比如 java.lang.SomeClass,因为它是小写的,它假设你正在寻找一个名为 system 的包。
回答by paxdiablo
It's System
with a capital S
:
它System
有一个大写S
:
class Demo {
public static void main (String[] args) {
System.out.println ("Hello");
}
}
The reason why the compiler is assuming it's a package name rather than a class or variable name lies in section 6.5
of the JLS, "Determining the meaning of a name"
:
为什么编译器是假设它的原因是包名称,而不是一个类或变量名位于段6.5
中的JLS,"Determining the meaning of a name"
:
The meaning of a name depends on the context in which it is used. The determination of the meaning of a name requires three steps.
First, context causes a name syntactically to fall into one of six categories: PackageName, TypeName, ExpressionName, MethodName, PackageOrTypeName, or AmbiguousName.
Second, a name that is initially classified by its context as an AmbiguousName or as a PackageOrTypeName is then reclassified to be a PackageName, TypeName, or ExpressionName.
Third, the resulting category then dictates the final determination of the meaning of the name (or a compilation error if the name has no meaning).
名称的含义取决于使用它的上下文。确定名称的含义需要三个步骤。
首先,上下文使名称在语法上属于以下六个类别之一:PackageName、TypeName、ExpressionName、MethodName、PackageOrTypeName 或 AmbiguousName。
其次,最初根据上下文分类为 AmbiguousName 或 PackageOrTypeName 的名称然后被重新分类为 PackageName、TypeName 或 ExpressionName。
第三,结果类别决定了名称含义的最终确定(如果名称没有含义,则编译错误)。
Your particular use is an AmbiguousName
, due to 6.5.1
:
您的特殊用途是AmbiguousName
,因为6.5.1
:
A name is syntactically classified as a MethodName in these contexts: (1) Before the '(' in a method invocation expression; (2) some other irrelevant contexts.
A name is syntactically classified as an AmbiguousName in these contexts: (1) To the left of the '.' in a qualified ExpressionName; (2) To the left of the '.' in a qualified MethodName; (3) some other irrelevant contexts.
在这些上下文中,名称在语法上被归类为 MethodName:(1) 在方法调用表达式中的 '(' 之前;(2)其他一些不相关的上下文。
在以下上下文中,名称在语法上被归类为 AmbiguousName: (1) 在 '.' 的左侧。在一个合格的 ExpressionName 中;(2) 在“.”的左边 在一个合格的 MethodName 中;(3)其他一些不相关的上下文。
Based on your code, system.out.println(whatever)
is a qualified MethodName
preceded by an AmbiguousName
. Later on in the process, 6.5.2
, the reclassification, mentioned earlier, takes place:
基于您的代码,system.out.println(whatever)
是一个合格的MethodName
由前AmbiguousName
。在此过程的后期6.5.2
,会发生前面提到的重新分类:
If the AmbiguousName is a qualified name, consisting of a name, a '.', and an Identifier, then the name to the left of the '.' is first reclassified, for it is itself an AmbiguousName.
If the name to the left of the '.' is reclassified as a PackageName, then if there is a package whose name is the name to the left of the '.' and that package contains a declaration of a type whose name is the same as the Identifier, then this AmbiguousName is reclassified as a TypeName.
Otherwise, this AmbiguousName is reclassified as a PackageName.
A later step determines whether or not a package of that name actually exists.
如果 AmbiguousName 是由名称、“.”和标识符组成的限定名称,则是“.”左侧的名称。首先重新分类,因为它本身就是一个 AmbiguousName。
如果'.'左边的名字 被重新分类为 PackageName,则如果存在名称为“.”左侧名称的包 并且该包包含名称与标识符相同的类型的声明,则此 AmbiguousName 被重新分类为 TypeName。
否则,此 AmbiguousName 将被重新分类为 PackageName。
后面的步骤确定该名称的包是否实际存在。
Because the reclassification walking up the tree (from println
towards system
) never results in a TypeName
, the default reclassification to PackageName
is always done.
因为沿着树向上走的重分类(从println
到system
)永远不会导致 a TypeName
,所以PackageName
总是进行默认的重分类。
That's why the error message you see is about a missing package rather than a missing class.
这就是为什么您看到的错误消息是关于缺少包而不是缺少类的原因。
回答by Tim Williscroft
The compiler says system package not found...
编译器说找不到系统包...
Because it can't find a package called system ( starting with a lower case s.)
因为它找不到名为 system 的包(以小写 s 开头)。
It might be able to find one called System, but that's not what your code asked for.
它可能会找到一个名为 System 的系统,但这不是您的代码所要求的。
The compiler almost never guesses.
编译器几乎从不猜测。
回答by shanu12joshi
THe only error in this code is that you have written small s instead of capital S in System.out.println("Hello");
这段代码唯一的错误是你在 System.out.println("Hello"); 中写了小 s 而不是大写的 S;