Search code examples
javaeclipsekryonet

Cannot import/reference class from external package


I have two packages mg.networktest and mg.networktest.server, both need access to the same class for registering/serializing classes that need to be send over the network. I think the best practice would be to create it on the server package and include it in the client.

However i cannot get the class referenced properly. I also tried adding another package but that is not allowed I think. But it just does not recognize my other package. The imports below all get The import mg.networktest.server cannot be resolved. I can get it to import mg.networktest.*; but that just references itself.

import mg.networktest.server; //This is the exact package
import mg.networktest.server.Network; //This is the physical class/.java name
import mg.networktest.server.Network.ChatMessage; //This is one of the classes inside the .java that needs to be included.

To be more clear:

  • Package 1: (mg.networktest.server) has a Network.java with class ChatMessage.
  • Package 2: (mg.networktest) needs the class ChatMessage from package 1.
  • Package 2: (mg.networktest) cannot do: import mg.network.server.*;.
  • How do i get access in package 2 (mg.networktest) to class ChatMessage from package 1 (mg.networktest.server)?

Solution

  • If your import statement causes a compilation error, verify the following:

    • You've used the correct syntax. For a single type name, use the syntax import TypeName;. To import type names on demand from a package or class scope, use the syntax import PackageOrTypeName.*;.
    • You've named a package and/or class that currently exists. Check the path, and check the spelling.
    • If the imported class is in a different Eclipse project or library, verify that it's on the build path for the importing class's project.
    • Rebuild the projects containing the importing and imported classes. Usually this isn't necessary if you have Build automatically enabled.