0

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)?
Madmenyo
  • 8,389
  • 7
  • 52
  • 99
  • 1
    To import the whole package use `import mg.networktest.server.*;` (`.*` doesn't work recursively), but you already imported that `Network` class so where is the current problem? – Tom Sep 21 '14 at 07:48
  • @Tom I cannot do that and i did not import the Network class, that is the problem i have. – Madmenyo Sep 21 '14 at 08:01
  • When I don't know that you're trying to achieve here and why it is not working. Which class exactly do you need and that is the error message you're receiving? – Tom Sep 21 '14 at 08:23
  • @tom i added a sum up of my problem. – Madmenyo Sep 21 '14 at 08:31

2 Answers2

2

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.
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • I just realized I incorrectly named my question. It should have been different project instead of package. When I read point 3 everything fell into place, added the project to the build path and now I can import it correctly. Is this the correct way to reference a single class? I mean, I added a complete project to another where I only need a single class from that project. Should i have put this "shared" class in a separate project and add that buildpath to both my main projects? – Madmenyo Sep 21 '14 at 09:00
  • 1
    Are you developing two executables, one for the client side and one for the server side? In that case, you would typically place the shared code in a separate library, included on path of both the client and the server. This specifies exactly what may be shared, and what is *not* shared. That in turns lets you know what will need to be re-installed if you make a change in any of the three projects. – Andy Thomas Sep 21 '14 at 09:13
1

I tried to rebuild your current project structure. This is my Network class with the inner class ChatMessage.

package mg.networktest.server;

public class Network {

  public class ChatMessage {
    public void message() {
      System.out.println("Blub");
    }
  }
}

And this is my class from package mg.networktest that needs the ChatMessage class

package mg.networktest;

import mg.networktest.server.Network;
import mg.networktest.server.Network.ChatMessage;

class NeedChatMessage {
  public static void main(String args[]) {
    final Network network = new Network();
    final ChatMessage chatMessage = network.new ChatMessage();
    chatMessage.message();
  }
}

You'll need to import the parent class Network and the inner class ChatMessage in order to use the inner class here. Now you'll need to create a new instance of the parent class Network first to access the inner class with that instance. May check this question Is it possible to create an instance of nested class using Java Reflection? and the answers there to find other ways to create a new instance of the inner class.

P.S.

You could also make the inner class ChatMessage static

public static class ChatMessage {
    public void message() {
        System.out.println("Blub");
    }
}

Now you don't need the Network class anymore to access the inner class

import mg.networktest.server.Network.ChatMessage;

class NeedChatMessage {
  public static void main(String args[]) {
    final ChatMessage chatMessage = new ChatMessage();
    chatMessage.message();
  }
}
Community
  • 1
  • 1
Tom
  • 16,842
  • 17
  • 45
  • 54