Can't use custom ClassLoader with ObjectInputStream

Hi all,
I want to transmit an object whose class is unknown to the receiver and whose class may not be loaded. Of course when the inputstream's readObject method tries to receive this class, it fails with a ClassNotFoundException. I have a custom FileClassLoader to dynamically load classes, this is tested and works.
I can dynamically load this class and prove that the class was loaded by calling class.newInstance() from the loader.loadClass method. But then when I try to read that same class from the input stream, it says ClassNotFoundException!
The code snippet looks like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FileClassLoader loader = new FileClassLoader(classFolder);
Class L = loader.loadClass(className); // no .class at end
System.out.println("New Instance "+L.newInstance().toString()); // works
ObjectInputStream eventStream = ... // stream from a socket
eventStream.readObject(); // throws class not found exception!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The only thing I can think is that the input stream only looks for classes loaded by the system classloader, could that be true? What other possibilities are there?
If this is more appropriate for another forum just let me know and I'll post it there.
Thanks in advance for any helpful hints anybody has!

I was thinking of something along the lines of
ObjectInputStream eventStream = (ObjectInputStream)Class.forName("java.io.ObjectInputStream", true, loader).newInstance();Of course, you would really have to get the proper Constructor for the class - and that just gets too long and stupid. Besides, I actually tried my suggestion, and it didn't work. I also tried setting the ContextClassLoader for the current thread (and creating a new thread with the ContextClassLoader set to "loader"). None of that works. Just setting the ContextClassLoader does not make the thread use it implicitly, it's apparently just a way to pass another ClassLoader to a thread, but it still needs to be used explicitly in a Class.forName call.
I did, however, try David's suggestions of overriding resolveClass in ObjectInputStream. That worked fine! Here's what I created:
import java.net.URLClassLoader;
import java.io.InputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.StreamCorruptedException;
public class MyObjectInputStream extends java.io.ObjectInputStream
    URLClassLoader myLoader = null;
    public MyObjectInputStream(URLClassLoader newLoader, InputStream theStream) throws IOException, StreamCorruptedException
        super(theStream);
        myLoader = newLoader;
    protected Class resolveClass(ObjectStreamClass osc) throws IOException, ClassNotFoundException
        System.out.println("Hey, I'm in my resolveClass");
        Class theClass = null;
        try
            theClass = Class.forName(osc.getName(), true, myLoader);
        catch (Exception e)
            System.err.println("An Error in my ResolveClass:");
            e.printStackTrace();
        return theClass;
}You then just create your ObjectInputStream with
ObjectInputStream eventStream = new MyObjectInputStream(loader, theSocket.getInputStream());

Similar Messages

  • How can I use custom sizes with the hp deskjet 3050a

    Why can't I find a place to cusomize card stock sizes in my printer

    The driver that is used for the Deskjet 3050a does not support custom sizes. It has a pre-set of choices and this is all the driver supports.
    You may be able to use an alternate driver, such as the HP Deskjet 9800 driver, that supports using custom paper sizes. Check the thread located here for information on how to use an alternate driver.
    Hope this helps.
    -------------How do I give Kudos? | How do I mark a post as Solved? --------------------------------------------------------
    I am not an HP employee.

  • Using custom classloader with javac

    There is an interesting thread going on at http://forums.java.net/jive/thread.jspa?threadID=13417&tstart=0.
    Since this forum seems to be more active than that forum, I wonder if anyone has an answer to my last post on that thread (post number 4 by garyh)?
    Thanks for any help.

    Please ignore this post.
    It was submitted before I finished writing it.
    Sorry,
    zganon

  • Can I use custom query in DB Adapter with Polling

    Hi All,
    I am getting problem while using db adapter with polling. Can I use custom query in db adapter with polling.
    I am using 2 tables; table 1 and table2
    Structure of table1 is as:
    File_name
    Batch_id
    Creation_date
    Status
    Structure of table2 is as:
    Batch_id
    Employee_ID
    Last_Name
    First_Name
    Middle_Name
    Group_ID
    Site_ID
    Dept_Num
    Report_id
    I have to use below query while polling to table1
    Select count(*) as NOSEXP, Report_id
    from table1, table2
    where table2.batch_id =
    table2.batch_id
    and table1.Status= 0
    group by Report_id;
    I mean, when I use DB adapter to poll table1, I have to use above query.
    Can you Please suggest on this.
    Thanks in advance.

    I will check for existance not based on Primary key but based on three other fields in the table. I think , by default DB adapter configured for MERGE checks for existance based on PK. Can we change the Primary Key in the mapping file after the DB adapter is configured. In that case , will it check for existance based on the fields , I mention.
    Edited by: user12020809 on Sep 16, 2012 6:08 PM

  • Can we use custom subtype (9001) in 0105?

    Hi,
    Can we user custom subtype 9001 in 0105 to store the user ids for ESS&MSS?
    I know by default the standard function modules of ESS will look for a user id in 0105 with subtype 0001. But 0001 has been used by some other portal pointing the same back end. we cannot use the same user id for ESS.
    For ESS, can we use custom subtype for the user ids? Is there any configuration to handle this scenario? How do we link the new portal should validate the user ids in custom subtype.
    Advance thanks for your valuable input.
    cheers
    Jayanthi

    Hi Kirsten,
    Thanks for your feedback.
    Definitly I dont want to make changes to any of the standard programs. Changing the existing portal is also a major impact in the project.
    Is there any configuration option available for ESS to point to the custom subtype 0105? We are in requirement gathering phase,the DEV environment is yet to be ready.
    If you find any info, please let me know.
    cheers
    Jayanthi

  • How can I use the ScrollTablePane with kodo ?

    How can I use the ScrollTablePane with kodo, taking a result Query as
    a Collection and throw it into the ScrollTablePane , if it not possible ,
    Is there a Component which I can do it ?
    Thanks , Marco Aurelio.

    Marco-
    I am not familiar with any "TableScrollPane" classes. Can you clarify
    whether this is some 3rd party custom component, or something you have
    written?
    Note that we do not provide any pre-build graphical data components with
    Kodo, but we do intend to provide some examples in the future.
    In article <cl1hn7$9ep$[email protected]>, Marco Aurelio Bueno wrote:
    >
    How can I use the TableScrollPane with kodo, taking a result Query as
    a Collection and throw it into the ScrollTablePane , if it not possible ,
    Is there a Component which I can do it ?
    Thanks , Marco Aurelio.--
    Marc Prud'hommeaux
    SolarMetric Inc.

  • How can I use the TableScrollPane with kodo

    How can I use the TableScrollPane with kodo, taking a result Query as
    a Collection and throw it into the ScrollTablePane , if it not possible ,
    Is there a Component which I can do it ?
    Thanks , Marco Aurelio.

    Marco-
    I am not familiar with any "TableScrollPane" classes. Can you clarify
    whether this is some 3rd party custom component, or something you have
    written?
    Note that we do not provide any pre-build graphical data components with
    Kodo, but we do intend to provide some examples in the future.
    In article <cl1hn7$9ep$[email protected]>, Marco Aurelio Bueno wrote:
    >
    How can I use the TableScrollPane with kodo, taking a result Query as
    a Collection and throw it into the ScrollTablePane , if it not possible ,
    Is there a Component which I can do it ?
    Thanks , Marco Aurelio.--
    Marc Prud'hommeaux
    SolarMetric Inc.

  • Can we use custom RFC in creating models in Visual composer??

    Dear Experts,
    Can we use custom RFC in creating models in Visual composer??
    If yes, kindly provide some documents or links which would guide me how to achieve it.
    Warm Regards
    Upendra Agrawal

    Hi,
    Yes,you can do it.
    Configure the rfc and use like others Standard BAPI procedure.
    [https://www.sdn.sap.com/irj/scn/wiki?path=/display/vc/connectivity]
    Regards,
    Govindu

  • How do i run an external monitor with my macbook and change settings so that when i close the lid the signal to the monitor is not lost and i can continue using the mac with a mouse and a wireless keyboard?

    How do i run an external monitor with my macbook and change settings so that when i close the lid the signal to the monitor is not lost and i can continue using the mac with a mouse and a wireless keyboard?

    No, nothing will prevent the computer from going to sleep when you close its display except third-party hacks that are designed to do exactly that. I strongly advise against using any of those, as they may interfere with successful entry into clamshell mode (and they carry other downside risks as well). Just wait until the computer is asleep (with its sleep light pulsing), then press any key on the keyboard. It sounds as though your setup is working as it's designed to do.

  • How can I use an iPad with two itunes accounts?

    How can I use an iPad with two itunes accounts?
    Hi there
    My partner has a works iPad, which has her works Apps on (ones that are specifically made by her company and are NOT available in the App store).
    What I want to be able to do is to ALSO have my itunes account on there so that I can put all the apps I have purchased previously from my iPhone on it.
    This is so I can put on VLC player (thats already in my itunes account) so I can add videos to the iPad.
    How do I do this please?
    I tried it before by connecting it to my mac, but it wiped everything off the iPad completely and also deleted the data settings for using 3G.
    So I want to avoid this happening again the future as it had to be sent back to her head office to be sorted again.
    Any advise please?
    Thanks

    You can't. The iPad is desgined to be a 'one user' device and only really works with one apple ID at a time.
    The only way would be if she, at work and with her work apple id, downloaded your apps (repurchasing them of course).
    It's not meant to be a multiple user/multiple ID device.

  • How can i use text expander with the new Mavericks,

    how can i use text expander with the new Mavericks,

    I regret upgrading to mavericks for the same reason
    ftamez wrote:
    how can i use text expander with the new Mavericks,
    Now I have been searching - and you have to buy an app and it will cost you $34.99 (what a rip off)

  • HT202213 can i use home sharing with my wife who has a different apple id

    Can I use home sharing with my wife who has a different apple id so we dont buy the same songs twice

    Wow, great question mhaney73.  There's got to be a way.  I have that problem as well, double songs all over the place. 

  • HT4363 Can I use home sharing with FaceTime?

    Can I use home sharing with FaceTime

    Winston,
    Thanks for the promt reply. But my problem was that I did not know how to use the air play. I looked around and figured it out.
    thanks,

  • How can we use Custom MessageBox in SelectionChangedEvent of LongListSelector for Windows Phone 8

    Dear Sir/Madam,
    How can we use Custom MessageBox in SelectionChangedEvent of LongListSelector for Windows Phone 8.
    Actually my problem is that When i am using Custom  MessageBox in SelectionChangedEvent of LongListSelector,when i am click Open(Left Button) it's working fine and navigated correctly,But when i am Click the No(Right Button) then it stayed in same page
    but all that page is in stuckup i mean that page is not working and not doing any event.
    My C#.net Code
    private async void userPageLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    if (e.AddedItems.Count > 0)
    if (userPageLongListSelector.SelectedItem == null)
    return;
    if (dbTenMin == null)
    dbTenMin = new Database(ApplicationData.Current.LocalFolder, "tenMInDBSchema.db");
    await dbTenMin.OpenAsync();
    var res = (sender as LongListSelector).SelectedItem as _10Min._10MinClass.minUserPages;
    var resIndex = (sender as LongListSelector).ItemsSource.IndexOf(userPageLongListSelector.SelectedItem);
    string selectedPageName = res.userPages.ToString();
    string selectedPageDesignUser = res.pageDesignUser.ToString();
    int selectedIndex = resIndex;
    CustomMessageBox messageBox = new CustomMessageBox()
    Caption = "Message...!",
    Message = "This form need offline datalist,Please load now.",
    LeftButtonContent = "Open",
    RightButtonContent = "No"
    messageBox.Dismissed += (s1, e1) =>
    switch (e1.Result)
    case CustomMessageBoxResult.LeftButton:
    string uidAndpwd = _10MinClass._10MinStaticClass.csUidAndPwd.ToString();
    _10MinClass._10MinStaticClass.csDataListPageDetails = selectedPageDataDetailsForSchema.ToString();
    _10MinClass._10MinStaticClass.csAllDataLists = offlineDataBaseDataListNam;
    _10MinClass._10MinStaticClass.csNotCreatedSchemaNameOfDBList = notCreatedDataLists;
    userPageLongListSelector.SelectedItem = null;
    if (dbTenMin != null)
    dbTenMin.Dispose();
    dbTenMin = null;
    NavigationService.Navigate(new Uri("/10MinformDataList.xaml", UriKind.Relative));
    else
    NavigationService.Navigate(new Uri("/10MinformDataList.xaml", UriKind.Relative));
    break;
    case CustomMessageBoxResult.RightButton:
    break;
    case CustomMessageBoxResult.None:
    break;
    default:
    break;
    messageBox.Show();
    Same custom messagebox code working in Phone_BackKeyPress event i am writing the code in Right Button that e.OriginalSource.ToString(); then it is working fine.
    But It is not working in Selection Changed Event in LongListSelector control in Windows Phone 8.
    Please help me,as soon as possible.
    Thanks & Regards,
    SrinivaaS.

    What happens if you leave the implementation for LeftButton empty as well , does the page gets stuck in that case also, if you press left button?
    i.e.
    CustomMessageBox messageBox = new CustomMessageBox()
    Caption = "Message...!",
    Message = "This form need offline datalist,Please load now.",
    LeftButtonContent = "Open",
    RightButtonContent = "No"
    messageBox.Dismissed += (s1, e1) =>
    switch (e1.Result)
    case CustomMessageBoxResult.LeftButton:
    break;
    case CustomMessageBoxResult.RightButton:
    break;
    case CustomMessageBoxResult.None:
    break;
    default:
    break;
    messageBox.Show();
    http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8

  • Can I use my macbook with a mac mini

    can I use my macbook with a mac mini

    You mean can you network the two Macs?
    What OS X version are you running on both of your Macs?

Maybe you are looking for

  • IPhone deletes notes after editing or making a new note?

    Hello everyone.  I've googled this to death and cannot find even so much as a hint.  My iPhone (iOS 6.0) today began doing the strangest thing regarding the Notes app.  I'll attempt to explain it as best I can.  I will add that my iPhone is set to no

  • Mail not showing up

    I have apples products at home and a pc at the office. When I send mail on my pc, it does not show up in my sent folders on my apple devices. Is there a way to do that? Does iCloud only work when things are done on apple products? Thanks!

  • Windows quicktime- memory faults causing non-response on 720p video MJPEG codec

    windows vista 64 home premium nvidea evga gtx260 with latest drivers 4 gb ram (2.5 gb free) code 2 quad Q9950 yorktown processor at 2.83 ghz more Hard drive than i will ever use.... trying to play HD 720p video recorded on casio camera. Quicktime pro

  • Columns justification in tables

    LabVIEW version 8.0 Is there any way to independently justify at ones, each column of a table, left or centre or right ? and to justify all columns in a table, for instance all left ? Simbani

  • Connecting to installing 10g on Windows XP 64 bit

    In can connect from sqlplus and trying to connect to same instant from visual studio.net. I aget ORA-12154. Any suggestion