Lakshmikanth Ambekar
Lakshmikanth Ambekar

Reputation: 25

Error in import statement Java

Following is the import statements in my code. The classes that am importing is in the same package comm.

package comm;
import WikiLinksReducer;
import WikiPageLinksMapper;
import XmlInputFormat;
import RankCalculateMapper;
import RankCalculateReduce;
import RankingMapper;

Am getting the following errors when I compile the code:

WikiPageRanking.java:2: '.' expected
import WikiLinksReducer;
                   ^
WikiPageRanking.java:2: ';' expected
import WikiLinksReducer;
                    ^
WikiPageRanking.java:3: class, interface, or enum expected
import WikiPageLinksMapper;
   ^
WikiPageRanking.java:4: '.' expected
import XmlInputFormat;
                 ^
WikiPageRanking.java:4: ';' expected
import XmlInputFormat;
                  ^
WikiPageRanking.java:5: class, interface, or enum expected
import RankCalculateMapper;
   ^
WikiPageRanking.java:6: '.' expected
import RankCalculateReduce;
                      ^
WikiPageRanking.java:6: ';' expected
import RankCalculateReduce;
                       ^
WikiPageRanking.java:7: class, interface, or enum expected
import RankingMapper;

Upvotes: 1

Views: 2881

Answers (3)

Torc
Torc

Reputation: 1322

I hope this can serve as a robust answer!

Java packages are meant to be a mechanism for managing naming and visibility of files.

Classes in a package will have "knowledge" of one another by default, and they will be unique from classes from external classes which appear to have the same name. The package name ensures that the naming convention is still unique. There are more nuances to Java packages, like limiting visibility in and between packages. Oracle has a decent tutorial for more specifics.

As for making a simple example, keep note of the following :

  1. The keyword package and the package name must be the first statement
  2. The package name is typically mirrored by the directory structure
  3. By default, Java will use the current working directory as its starting point for execution. This means that sub-directories can be found.
  4. You should execute the program in the directory above your working directory, or set the CLASSPATH variable to include a path to your package directory.
  5. Each file will need to be compiled (obviously)
  6. After compilation, you can run your program in the directory above the package directory, but remember you must add the package name + "." to your class name
  7. You CAN import a class file in the same package, but you don't NEED to

Example directory structure :

C:\java\mypackage
C:\java\mypackage\AccountBalance.java
C:\java\mypackage\Balance.java
C:\java\mypackage\Thing.java

Files :

AccountBalance.java
package mypackage;

import mypackage.Thing; // unnecessary import, NOTE : name needs to be qualified w/ package
class AccountBalance {
   public static void main(String args[]) {
      Balance balance = new Balance("Jack", 123); // SAME package --> no need to import
      balance.show();

      Thing thing = new Thing("Jill");
      System.out.println(thing.getName());
   }
}
Balance.java
package mypackage;

class Balance {
   String name;
   double balance;

   Balance (String name, double balance) {
      this.name = name;
      this.balance = balance;
   }

   void show() {
      if (balance >0) {
         System.out.print("-->");
         System.out.println(name + " : $" + balance);
      }
   }
}
Thing.java
package mypackage;

class Thing {
   String name;

   Thing(String name) {
      this.name = name;
   }

   public String getName() {
      return this.name;
   }
}

Compilation & Execution Steps :

C:\java\mypackage> javac AccountBalance.java Balance.java Thing.java 
C:\java\mypackage> cd ..
C:\java> java mypackage.AccountBalance
Notes on Execution :
  1. Can compile multiple classes in one line, and must if there are inter-dependencies with multiple files. You can definitely put all of these classes within the same AccountBalance.java file and compile just by doing javac AccountBalance.java but that gets messy as the files grow.
  2. Running the application requires mypackage.AccountBalance to "name" everything correctly. AccountBalance doesn't exist from the run time's perspective. Nor does mypackage.AccountBalance.java

Upvotes: 0

DesirePRG
DesirePRG

Reputation: 6378

The import statement is used to import classes from packages other than the package which your class is. You do not need to import packages if the other classes you use are in the same package.

Upvotes: 0

Michelle
Michelle

Reputation: 2900

You don't need to import classes in the same package as the current class, they're automatically imported.

Upvotes: 2

Related Questions