If child class not it classpath

Discussion in 'Java' started by Aaron Fude, May 18, 2008.

  1. Aaron Fude

    Aaron Fude Guest

    Suppose that on the "server", I have the following code:

    ObjectInputStream in = new
    ObjectInputStream(coming.from.wherever);
    ClassA obj= (ClassA) in.readObject();

    and I send via the input stream an instance of ClassB that extends
    ClassA, but ClassB is not in the classpath of the "server". Should
    this work? I would have thought "yes", but I tried it and the answer
    is "no".

    I have found a workaround so that's not what I am looking for, but I'm
    hoping that, through an explanation, I will learn something new.

    I see a little bit of what's wrong here. The "server" may want to call
    a function on "obj" that's been overridden in ClassB and it doesn't
    have access to that code. But is there a way to supply the code the
    the "server" as well?

    Thanks!

    Aaron
     
    Aaron Fude, May 18, 2008
    #1
    1. Advertising

  2. Aaron Fude

    Arne Vajhøj Guest

    Aaron Fude wrote:
    > Suppose that on the "server", I have the following code:
    >
    > ObjectInputStream in = new
    > ObjectInputStream(coming.from.wherever);
    > ClassA obj= (ClassA) in.readObject();
    >
    > and I send via the input stream an instance of ClassB that extends
    > ClassA, but ClassB is not in the classpath of the "server". Should
    > this work? I would have thought "yes", but I tried it and the answer
    > is "no".


    You can not deserialize an instance of a class that you do not have
    a class definition for.

    Because it does not know about the members.

    Arne
     
    Arne Vajhøj, May 18, 2008
    #2
    1. Advertising

  3. Aaron Fude

    Aaron Fude Guest

    On May 18, 5:14 pm, Arne Vajhøj <> wrote:
    > Aaron Fude wrote:
    > > Suppose that on the "server", I have the following code:

    >
    > >     ObjectInputStream in = new
    > > ObjectInputStream(coming.from.wherever);
    > >     ClassA obj= (ClassA) in.readObject();

    >
    > > and I send via the input stream an instance of ClassB that extends
    > > ClassA, but ClassB is not in the classpath of the "server". Should
    > > this work? I would have thought "yes", but I tried it and the answer
    > > is "no".

    >
    > You can not deserialize an instance of a class that you do not have
    > a class definition for.
    >
    > Because it does not know about the members.
    >
    > Arne


    That makes sense. And it's not possible to send the definition of the
    class over, too?
     
    Aaron Fude, May 18, 2008
    #3
  4. Aaron Fude

    Arne Vajhøj Guest

    Aaron Fude wrote:
    > On May 18, 5:14 pm, Arne Vajhøj <> wrote:
    >> Aaron Fude wrote:
    >>> Suppose that on the "server", I have the following code:
    >>> ObjectInputStream in = new
    >>> ObjectInputStream(coming.from.wherever);
    >>> ClassA obj= (ClassA) in.readObject();
    >>> and I send via the input stream an instance of ClassB that extends
    >>> ClassA, but ClassB is not in the classpath of the "server". Should
    >>> this work? I would have thought "yes", but I tried it and the answer
    >>> is "no".

    >> You can not deserialize an instance of a class that you do not have
    >> a class definition for.
    >>
    >> Because it does not know about the members.

    >
    > That makes sense. And it's not possible to send the definition of the
    > class over, too?


    In theory yes.

    It just requires a classloader that can read what is loaded
    from the stream.

    It will be slightly easier if you can use HTTP to load
    the class file, because a standard URLClassLoader can
    load over HTTP.

    Arne
     
    Arne Vajhøj, May 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flip
    Replies:
    3
    Views:
    7,381
    Tony Morris
    Feb 9, 2004
  2. Gabe
    Replies:
    3
    Views:
    1,107
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,182
    David Schwartz
    Dec 9, 2003
  4. Replies:
    2
    Views:
    782
  5. Replies:
    2
    Views:
    1,142
    Mike Schilling
    Mar 2, 2008
Loading...

Share This Page