Avatar of Member_2_2394978
Member_2_2394978Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Class not found - ObjectInputStream

Hi,

I have the attached code to access a file I have serialized using another programme.

Although I get ClassNotFoundException: DataStore on the line 'data = (DataStore) ...'
This happens when i replace the cast with CG2FishEye.DataStore which includes the package the class is in.

Any Suggestions
Thanks
James
DataStore data = null;
           FileInputStream fis = null;
           ObjectInputStream in = null;
           try {
                fis = new FileInputStream(fileChooser.getSelectedFile());
                in = new ObjectInputStream(fis);
                data = (DataStore) in.readObject();
                in.close();
            }
            catch(IOException ex) {
                ex.printStackTrace(); }
            catch(ClassNotFoundException ex) {
                ex.printStackTrace();
            }

Open in new window

Java

Avatar of undefined
Last Comment
CEHJ
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You must

import java.io.ObjectInputStream;
Avatar of Member_2_2394978

ASKER

Sorry didn't include that in my code snipet, I have this imported.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Sorry the title of your question misled me. The class you're deserializing must be in the classpath
Avatar of Member_2_2394978

ASKER

How do you mean?

Does it make a difference that in the programme I serialize the class is in a different package to the one when i deserialize?
Avatar of Member_2_2394978

ASKER

Ok it seems to be that it is in different packages!

Which means that the classes are different in each programme, because in one they include a "package xxx;" line at the top and in the other it doesn't!

Anyway to get around this?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

... and each is different again from

SomeClass
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>Anyway to get around this?

No. The class you serialize must be the one you deserialize
Avatar of Member_2_2394978

ASKER

Ok cool i'll move any classes needed for the serialization and deserialization into a package called Data - Then usethis package within both programmes.  Hopefully that'll work.

Thanks Very Much.
James
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

:-)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo