user2800912
user2800912

Reputation: 123

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

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:

import java.lang.Math;

package test1;

This error is popping up under "package test1;"

Upvotes: 10

Views: 35841

Answers (2)

Reimeus
Reimeus

Reputation: 159844

Place the package declaration before the import statement

package hw1;

import java.lang.Math;

The import statement itself is unnecesary as all classes in java.lang are imported by default.

Read Creating a Package

Upvotes: 3

rgettman
rgettman

Reputation: 178303

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

package hw1;

import java.lang.Math;

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

The JLS, Chapter 7 says:

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.

Upvotes: 19

Related Questions