Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:19:39+00:00 2026-05-30T14:19:39+00:00

Okay, so I am trying to do my network packet handling in Java using

  • 0

Okay, so I am trying to do my network packet handling in Java using classes. For reading data from my stream I use a DataInputStream. My reading thread for my server looks like this:

public void run()
    {
        while(client.isActive())
        {
            try{
                handle(is.readShort());
            }catch (IOException e){
                client.stop(e);
                break;
            }
        }
    }

Now I’ve got a method handle:

public void handle(short id) throws IOException
{
    InPacket packet = null;

    try {
        packet = ClassUtils.newInstance(InPacket.class, "server.client.InPacket"+id);
    }catch (Exception e){
        e.printStackTrace();
    }

    if (packet!=null){
        packet.handle(this);
    }
    else{
        throw new IOException("Invalid packet");
    }
}

I try to instantiate a new class using the

packet = ClassUtils.newInstance(InPacket.class, "server.client.InPacket"+id);

line.

In ClassUtils this is that function:

public static <T> T newInstance(Class<? extends T> type, String className) throws
ClassNotFoundException,
InstantiationException,
IllegalAccessException {
    Class<?> clazz = Class.forName(className);
    Class<? extends T> targetClass = clazz.asSubclass(type);
    T result = targetClass.newInstance();
    return result;
}

My problem is: when I try to get that class with only part of the name (I try to get it by “InPacket1”, while the class is called “InPacket1Connect”), it can’t find it. Is it possible to do this in Java, and if so how? If not, what method do you recommend for handling my network packets?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:19:40+00:00Added an answer on May 30, 2026 at 2:19 pm

    An alternative approach would be to use a map (or enum) which maps the id to the full class name.

    Pulling in the stuff from the comments, ensure that this mapping class is available as a jar (or may be in the same jar which contains the implementations of the packet handlers) as your “messaging layer” jar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I'm trying to load a file in Java using this code: String file
Okay, what have I done wrong here? I'm trying to .POST data from a
OKay I don't use actionscript and trying to help my friend edit a flash
Okay, here's what I'm trying to do: I'm trying to use PHP to develop
Okay, I feel like a bit of a 800 pound gorilla trying to thread
Okay so I am trying to use a simple UINavigationController with iPhone SDK in
Okay, I am trying to use OCI8 with PHP 5.3.x and Oracle 10g and
Okay I am trying to make minecraft mods using eclipse. I used this video
Okay so I'm trying to learn Java client/server stuff, and am going through the
Okay so I am trying to use range for input type Times: <input type=range

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.