令牌“包”上的语法错误,需要导入(Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18926806/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:34:19  来源:igfitidea点击:

Syntax error on token "package", import expected (Java)

java

提问by user2800912

I am trying trying to import java.lang.Math in Java on Eclipse and am getting the error in the title. Here is the beginning of my code:

我正在尝试在 Eclipse 上的 Java 中导入 java.lang.Math 并且在标题中遇到错误。这是我的代码的开头:

import java.lang.Math;

package test1;

This error is popping up under "package test1;"

这个错误在“package test1;”下弹出

采纳答案by rgettman

The packagestatement must be first in the file, before anything, even imports:

package语句必须在文件中的第一个,甚至导入之前:

package hw1;

import java.lang.Math;

Plus, you don't need to import java.lang.Math, or anything in java.langfor that matter.

另外,您不需要 importjava.lang.Math或任何java.lang内容。

The JLS, Chapter 7says:

JLS,第7章说:

A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.

编译单元自动访问其包中声明的所有类型,并自动导入预定义包 java.lang 中声明的所有公共类型。

回答by Reimeus

Place the package declaration before the import statement

将包声明放在 import 语句之前

package hw1;

import java.lang.Math;

The importstatement itself is unnecesary as all classes in java.langare imported by default.

import语句本身是不必要的,因为java.lang默认情况下会导入所有类。

Read Creating a Package

阅读创建包