Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,454 members, 7,816,050 topics. Date: Friday, 03 May 2024 at 01:07 AM

J2se/ Android Complication - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / J2se/ Android Complication (2932 Views)

Learn Java Programming (J2SE, J2EE) At Chora Technologies Ltd, Abuja. (2) (3) (4)

(1) (Reply) (Go Down)

J2se/ Android Complication by Zeta(m): 11:38pm On Aug 31, 2011
Hi, everybody,

I created JSSE application that uses a serializable class named StoryNode. This ‘StoryNode’ class needs only the java.io.* import statement. The class that uses this ‘StoryNode’ class has a Vector object that holds the the ‘StoryNode’ objects. This Vector object is serialized to a file(with a .str file extension) which another Android application I wrote can read.

So I copy this file to the assets directory of my android project. I can get an Inputstream to the file from within an Activity class with the following code:
InputStream in = getAssets().open("astorytree.str"wink;

But there’s a problem. In the android application when I try to deserialized the inputstream to a Vector object an exception is thrown saying: java.lang.ClassNotFoundException: StoryNode

Within my android project I have already defined the StoryNode class, so I don’t understand why the StoryNode class can’t be found. Even though android uses the Dalvik virtual machine, its still the JVM that’s there(I think) so I don’t know if there is some incompatibility with a class defined using pure J2SE and the very same class defined in an android project.

Thanks in advance.
Re: J2se/ Android Complication by SayoMarvel(m): 11:17pm On Sep 01, 2011
I don't do Android app but I'm familiar with serialization. Did you define serial version unique identifier for your StoryNode class?
Something like
private static final long serialVersionUID = 1234L;

If not, then define it in the two classes (J2SE and Android) and reserialize. I hope it helps.
Re: J2se/ Android Complication by logica(m): 3:53am On Sep 02, 2011
First the design of your application follows the Producer-Consumer Pattern. Your J2SE application is the Producer while the Android app is the Consumer.

Your Android app runs within the context of a JVM separate from your "producer" application therefore the Android app cannot automatically pick the classes in your "producer" app. What you need to do is either:

1. Export your classes in the "producer" app as a JAR file, create a /lib directory under your Android project, and then copy this JAR file to the /lib directory.
2. Create a utility Java project which both the "producer" and the Android app have dependency on. This utility Java project can then be exported as in 1 above to both apps.

The benefit of 2 is, you have already factored out all shared classes. With one, you may end up with a huge JAR file and quite a bunch of classes which are not shared could end up on the Android app (which by the way could lead to security issues).

Good luck.
Re: J2se/ Android Complication by SayoMarvel(m): 9:24am On Sep 02, 2011
@logica I'm confused. Are you trying to say that if an object graph is serialized on JVM A, JVM B will not be able to deserialize it? Even though JVM B has access to the serialized class and every other class in the graph?
I've done a bit of socket programming. Here is a very typical scenario; client creates a Command, serializes it, ships it across the wire to the server. Server deserializes the command, processes it, creates a result, serializes it, ship it across the wire to the client. Client deserializes the result and makes use of it.
The server and the client both run on different computers (JVM) but they still deserialize each other's outputs convinently.
I don't do Android programming but are you saying that I can't establish a simple network communication between J2SE and Android using serialized objects over sockets?
Re: J2se/ Android Complication by logica(m): 9:36am On Sep 02, 2011
If the target JVM has no class definition for the serialized entity, how do you think it will create an instance? I thought my explanation was simple enough. It does not matter than you serialize from A to B successfully; what matters is, does B have the definition for the class to instantiate the class? This is general Java and is not specific to Android.
Re: J2se/ Android Complication by Zeta(m): 11:19pm On Sep 02, 2011
Thanks friends for your spirited contributions. I really really appreciate it. I'll put some of your advice to work. My problem came from what I wanted to do for this african mobile apps competition , it ends this month , maybe its something you guys may be interested in. Here's the link: http://www.africanmobileapps.com/

If I have more trouble I'll say so. Thanks again.
Re: J2se/ Android Complication by Zeta(m): 12:08am On Sep 03, 2011
I didn't even know that what I was doing was following the 'Producer-Consumer' pattern. I guess I can't run away from Design Patterns any longer. The only one I remember a bit of is the 'Singleton'
Re: J2se/ Android Complication by SayoMarvel(m): 1:48pm On Sep 03, 2011
logica:

If the target JVM has no class definition for the serialized entity, how do you think it will create an instance?

@poster already said this in the post.
Zeta:

I created JSSE application that uses a serializable class named StoryNode. . . This ‘StoryNode’ class needs only the java.io.* import statement. The class that uses this ‘StoryNode’ class has a Vector object that holds the the ‘StoryNode’ objects. This Vector object is serialized to a file(with a .str file extension) which another Android application I wrote can read. . .

But there’s a problem. In the android application when I try to deserialized the inputstream to a Vector object an exception is thrown saying: java.lang.ClassNotFoundException: StoryNode

Within my android project I have already defined the StoryNode class, . . .

I think the guy has all the classes he needs.
@Poster, is the StoryNode source file in the two Applications exactly the same?
@logica Once again, I've never written a single line of Android code and I hope to learn from this. Looking forward to your reply.
Re: J2se/ Android Complication by logica(m): 2:08pm On Sep 03, 2011
SayoMarvel:

@poster already said this in the post.
I think the guy has all the classes he needs.
@Poster, is the StoryNode source file in the two Applications exactly the same?
@logica Once again, I've never written a single line of Android code and I hope to learn from this. Looking forward to your reply.
See, that is the problem with using an IDE if you are a novice. It might end up obfuscating the concepts.  A workspace such as you have in Eclipse or Netbeans allows you to group related projects together, but it also give you the opportunity to create unrealistic dependencies. What I mean by that is this:

Your workspace in this kind of project will start off with 2 projects:

1. The Android client (Consumer)
2. The server (Producer)

In the real world, they are not within the same physical space, meaning they do not share the same Java Run-time: the Android client runs on a phone while the server runs on some machine which could literally be anywhere in the world.

But when you put them in the same workspace, as a novice, since the Android client requires the StoryNode class, the project will indicate that the class is missing. But this class already exists in the server project right? So to handle this missing dependency you will tend to add the server project as a dependency, or add the class from the server as a dependency. But this does not make sense because in the real world the Android client runs within a completely different Java Run-time from the server. In other words a direct dependency between the Android client and the server does not exist in the real world.

At build time, this will not appear to be a mistake, but the mistake will be apparent at run-time when the client actually requires the StoryNode class and searches for it in its own Java Run-time, since it has no access to the Run-time of the server.

So the correct thing will be to model this separation (by creating a utility project which they both share and can bundle separately). You have to always have this at the back of your mind.

As I earlier said, this is not an issue particular to Android, but it applies to any application similar in design (for instance this also applies to an Applet client as well). But the Android Test Device does a good job of modeling the real life thing in that the Java Run-time is guaranteed to be different from the Java Run-time of the server; which might not be the case if the client is a standard Java Application.
Re: J2se/ Android Complication by SayoMarvel(m): 10:26pm On Sep 03, 2011
Okay, thanks for that. This is where experience really pays off. *keep-the-coding-hat-on*
Re: J2se/ Android Complication by Zeta(m): 10:58pm On Sep 03, 2011
Hi guys.

Adding a serialVersiouUID in both StoryNode classes didn't work , still the same ClassNotFoundException runtime exception thrown. I'm glad it was suggested, though, because I learnt something new when i did a little bit of research on it in google. All the JavaSE books,(maybe five or more) I have, don't even whisper anything about serialVersionUID, and it turned out to be quite an important thing to know when serializing objects.

I use eclipse for the android part of the project and use TextPad for the J2SE part.
logica writes like a very smart somebody, but what he wrote seems far too intricate for me to get.

I've opted for representing my collection of StoryNode objects as xml. This way no effing ClassNotFoundException will occur. A friend adviced that I do this about a month back, but I felt serializing was just too painless for me to let go.

I came up with an algorithm to get my collection of data objects back in the android reader program, but found out the algorithm was not perfect. I then figured that a better way would be to replay the entire process of addition and deletion of StoryNodes in using the J2SE program, when reading back the data in the android program. Its just crazy enough to work.

Will post more questions when I have troubles. Thanks guys.
Re: J2se/ Android Complication by logica(m): 11:18pm On Sep 03, 2011
ClassNotFoundException definitely is clear enough (the JVM is telling you it can't find the definition of a class it needs); so you should have known adding serialVersionUID will not solve the problem. That will only solve the problem of identity/version tracking.

If you decide to use a simpler method, that's fine. But you need to learn the basics now (when you are still a novice). Running away from them will not help you (if you intend to actually grow as a programmer).
Re: J2se/ Android Complication by SayoMarvel(m): 6:07pm On Sep 04, 2011
I've also wanted to suggest a "data bridge" like xml, txt etc as a simple back-door before but as @logica said, don't run awa from good practice. I had to learn those modules stuffs not quite long ago when I started developing for the NetBeans platform.
Re: J2se/ Android Complication by NumberOne2(m): 10:46pm On Sep 04, 2011
The DVM and JVM aren't the same. The codes will be compiled differently. I had this problem once working with Blackberry apps. I'll advice you just write your codes for Android and compile with the DVK.
Re: J2se/ Android Complication by Zeta(m): 11:25pm On Sep 04, 2011
I think 'Number One' may be closer to the truth because the code for the 'StoryNode' class in the android project is EXACTLY the same as that in the J2SE program, but the problem still pops up. What I need done is very simple. I know I'm a novice, but I think it is best to find the easiest and shortest way to solve a problem. I think that's one of the things that differentiates an expert from a novice. Fine, the expert can do more complex stuff than a novice, but if they were to attempt the same thing, the expert would get it done in the easiest, shortest and most elegant way possible.

Anyway, using xml got the job done, though I had to change my algorithm for re-constructing my data in the android program. Thanks again guys.
Re: J2se/ Android Complication by logica(m): 11:36pm On Sep 04, 2011
I have told you why the "problem" pops up, but it's left to you to figure. It has nothing to do with compilation with DVK or what not.
Re: J2se/ Android Complication by Zeta(m): 11:58pm On Sep 04, 2011
And I have told you that there isn't a problem anymore, and that is left for you too to figure. Oga no vex for ma head.

Even though, I'm writing both programs on the same computer, they are both written with different IDEs/text-editors, so your dependency whatever-stuff is not applicable. Thanks for the advice anyway. You may think you are right, but how many times have you written code that made the complier complain? I imagine millions. So, you could still be wrong about your diagnosis of my now non-existent problem. Yes, I'm right about this, yes , Thanks again everyone.
Re: J2se/ Android Complication by logica(m): 12:12am On Sep 05, 2011
LOL. Sorry but I cannot "vex" over your problem. But I'll say this only once - you think you don't have a problem anymore, but I think you have a bigger problem if you cannot grasp simple concepts. That is the part you should really get. Good luck.
Re: J2se/ Android Complication by Zeta(m): 1:01am On Sep 05, 2011
@logica It is ON , I'm new at this online beefing thing, so don't laugh , You are very condescending, you know that? You tell me I have a "bigger" problem in grasping simple concepts , how do you want me to feel about that when in my heart of hearts I know this to be true and you've just let me know how dumb I really am? If I commit suicide because of this it'll be on your head , and my mom will have your head , for realziz.

Then, you later tell me goodluck , like you mean 'Goodluck, I hope you fix your head, so that you can grasp simple things.'' You are a very hurtful person. I hate you. I hate you very much. I hope you continue to help me in my other problems, though. Thanks. I don't have a religion, so I won't say 'God punish.' Don't know if that line ever work for anyone.

You've been coding for a few years, and completed a few projects, so you think I'm a novice(which I am). So you are the master, right, someone who has "grown" as a programmer? I'm learning I agree, but don't discourage me. See, oga, you are not as great a programmer as you think you are. Have you written a programming book? Have you become a millionaire from your coding efforts? Have you won an international coding challenge? We, humans tend to have a bloated image of ourselves, so don't get it twisted.

BEEF OFF:
Words seem, TO ME(I'm being agnostic here), quite powerful. You don't use yours well, from my subjective point of view. What you may be explaining may be simple for you, but it isn't simple for everyone. If Einstein has thought us anything, it is the relativity of things we are beyond doubt convinced are absolute. I didn't understand this when I used to teach my brother math in secondary school. I understand it now 5 years later. Not everybody is moving at the same high intellectual speed as you 'logica'. On a final note, put more effort in making your ideas clear and as simple as you can, but not simpler(cos it would be impossible for you, since you can't make something SIMPLER THAN YOU COULD).
Re: J2se/ Android Complication by Fayimora(m): 11:08pm On Sep 08, 2011
Ok for some reason i DO NOT understand this thread. No offence to the OP.

Zeta what exactly are you trying to do. I develop Android apps but for fun! What are you battling with. PLease just say what you are trying to do, where you got stuck and the error you are having,  Would be easier that way for me, can't read this whole thread again. . .
Re: J2se/ Android Complication by Zeta(m): 4:14am On Sep 14, 2011
@Fayimora my mtn internet connection hasn't been working for the past three days, so I couldn't reply soon enough. I have a new problem with my Android program now, but here's the story so far:

I've created a J2SE application that can be used to create what I call story-tree files, they have a .str file extension(which is just a glorified xml file). Story-tree consist of nodes that can have text saved in them. I want the text in the .str file to be readable by an android program. The J2SE program uses a JTextPane for inputting text, while the android program uses a TextView for displaying text. The problem I had that started this thread has been more or less solved, but here's the thing:

I've noticed that when using the J2SE program to finally save your work(the text in the story nodes are saved as Nodes in xml) things like 'new lines' are represented(in the created .str file) as 
 . Any time I set the text of a TextView, with a sample text, in an xml Node, like the one below:


Whatever you like
I am the green lantern

nothing is displayed in the TextView.

For a sample text in an xml Node, like the one shown below:

WhateverWhateverWhatever

MoreWhateverWhateverWhatever

only 'WhateverWhateverWhatever' is displayed in the TextView.

I think the presence of the '
' characters causes it, cos when I remove them from the file the main text can be displayed(but the intended formating is lost). Please help figure this out. Thanks.
Re: J2se/ Android Complication by Javanian: 4:22pm On Sep 01, 2012
SayoMarvel: I don't do Android app but I'm familiar with serialization. Did you define serial version unique identifier for your StoryNode class?
Something like
private static final long serialVersionUID = 1234L;

If not, then define it in the two classes (J2SE and Android) and reserialize. I hope it helps.

i know this thread is one year old today...but why did you suggest 1234L??...couldnt it have been 1L or 2L??...does anyone know a reason for this?

(1) (Reply)

Cherrypy, Flask And Other Python Micros..... / Solve these challenges to join Our Cybersecurity Team / Reasons Why Nigerian Tech Companies Pay Poorly.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 80
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.