I am 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.
6 Answers
System.out.println("Hello"); You need a capital S
Package names are lower case like java.lang.SomeClass, since it's lowercase it assumes you're looking for a package named system.
2Why 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.
And (I guess) it is so because this is the better guess. 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.
Update (for the sake of completeness)
From @paxdiablo's answer below:
4The 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"
It's System with a capital 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":
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).
Your particular use is an AmbiguousName, due to 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.
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:
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.
Because the reclassification walking up the tree (from println towards system) never results in a TypeName, the default reclassification to PackageName is always done.
That's why the error message you see is about a missing package rather than a missing class.
2THe only error in this code is that you have written small s instead of capital S in System.out.println("Hello");
The most case in which you get an error Package system not found in system.out.println(" "); is because, you have to give your file name as "nameofpublicclass.java".
In this example, file name must be "Demo.java".
I think that will solve the error.
The compiler says system package not found...
Because it can't find a package called system ( starting with a lower case s.)
It might be able to find one called System, but that's not what your code asked for.
The compiler almost never guesses.