3

I have some projects in my workspace :

AndroidMonitoring # an android application
MonitoringModel # an android library project
DataServlet # servlet project

AndroidMonitoring (which depends on MonitoringModel,

in the Android section of the project properties)

compiles and runs just fine but I need the MonitoringModel classes to be available also in the DataServlet project. I added the Model as a dependency in the Java Build path of the DataServlet project but I get :

java.lang.NoClassDefFoundError: gr/uoa/di/monitoring/model/Battery
    gr.uoa.di.monitoring.server.servlets.DataCollectionServlet.doGet(DataCollectionServlet.java:20)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I need the Model to be an Android library project as it contains android classes - but also contains the methods to parse the files in the servlet - is it possible ? How should I set this up ?

EDIT : MonitoringModel is here

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361

2 Answers2

2

Solved !

  1. remove the dependency from DataServlet's java build path
  2. go to the MonitoringModel project and remove the library attribute, run it as an Android app remake it into a library (from here) Clean the MonitoringModel project
  3. grab the monitoringmodel.jar from bin/ and drop it into the DataServlet/WEB-INF/lib
  4. refresh and run on server
  5. done !

Will try and improve on this hack (linking to an external jar did not seem to work btw) - any better ideas will be accepted as an answer - however closing this for now.

EDIT : apparently step 3. can be substituted by creating a hard link from DataServlet/WEB-INF/lib/monitoringmodel.jar to monitoringmodel.jar - still testing this as some action sequences break the link methinks. Symbolic links do not seem to work though - reported this as a bug

EDIT2 : the steps below seem to work too - but I leave the manual procedure as it definitely works

  1. remove the dependency from DataServlet's java build path
  2. Hard link the monitoringmodel.jar from bin/ and to the DataServlet/WEB-INF/lib. I used shell link extension but this :

    mklink /H c:\path\to\WebContent\WEB-INF\lib\monitoringmodel.jar c:\path\to\bin\monitoringmodel.jar
    

    should also work

  3. Now everytime you make a change in monitoring model the jar is updated. You only have to refresh the servlet project (will be redeployed on server on its own by default)

Clarification : of course the servlet project is not meant to use android.* classes - this was not my issue - my issue was to have the model code in one place and this place had to be an android library

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
0

First of all - I believe Java Web Project will not work with any of Android specific classes due to many reasons.
If your MonitoringModel contains some Java code that you want to share between Android and Web application you can extract it to separate Java project and use a Link Source option in Properties->Build Path to link it to both projects. enter image description here

Viacheslav
  • 5,443
  • 1
  • 29
  • 36
  • No the classes I want to share contain both android and pure java imports. Any way I can create a library project that can be used by both the android application and the web project ? – Mr_and_Mrs_D Aug 27 '13 at 17:28
  • No way. Your web project has no access to the Android SDK and shouldn't have. Your problem is in the design, just accept a different design. Problem solved. –  Sep 01 '13 at 06:24
  • @YekhezkelYovel: You have to add @ for me to get notified - can you elaborate on what you say ? I could make the servlet project an android project too for instance - can you suggest a different design ? The MonitoringModel is [here](https://github.com/Utumno/MonitoringModel) – Mr_and_Mrs_D Sep 01 '13 at 17:08
  • @Mr_and_Mrs_D when you make your project an android project you don't put the SDK inside, only stubs. It only tells your complier what would happen on an actual device. If you than run it outside the Android OS youl recieve funny "stub" exceptions. Have a look at this thread: http://stackoverflow.com/questions/3896064/how-to-develop-using-android-libraries-in-a-simple-java-project-without-using-d –  Sep 02 '13 at 06:21
  • @Mr_and_Mrs_D sorry I can't suggest any better design, because I don't have time to dive into your project, but the fact that you actually need the same classes in two projects screams "coupling!!!" perhaps you should loosen those couples a bit. If you can, maybe work with some abstract classes or interfaces to allow for different implementations for each project. –  Sep 02 '13 at 06:31
  • @Yekhezkel: thanks - re: coupling : the model project contains the classes that "serialize" and (todo) deserialize my data. The android project serializes and sends to server while the server deserializes - so they should be in the same project methinks – Mr_and_Mrs_D Sep 02 '13 at 12:25
  • @Mr_and_Mrs_D I disagree. 1. If your android project only serializes and your server only desirializes, than your serializing class should be in your android project and your deserializing class should be on your server. 2. If both projects do both things than both classes should be in both projects. I don't think this is a good reason to make your android code dependent on your server code or the opposite. –  Sep 02 '13 at 12:38
  • @YekhezkelYovel: 2. I do not make my android project dependent on my servlet or the opposite - I make them both dependent on Model. 1. Serialization and derialization use common logic, constants, enums etc - those should be in the same classes not copy pasted in different classes. I do not mean to say the design is flawless but do have a look (derialization is missing) – Mr_and_Mrs_D Sep 02 '13 at 16:45
  • @YekhezkelYovel: so for instance in https://github.com/Utumno/MonitoringModel/blob/master/src/gr/uoa/di/monitoring/model/Position.java the `enum LocationFields` contains the order of the fields in the serialized object as well as the code to construct each of those (`getData()`) - this is the android dependent code. Anyway (that goes to Vyacheslav too) - please elaborate on the impossibility of doing what I want - so I can close this – Mr_and_Mrs_D Sep 02 '13 at 17:14
  • 1
    @Mr_and_Mrs_D you wrote "use common logic, constants, enums etc - those should be in the same classes not copy pasted in different classes" - so take enums, constants and pure java common logic to separate java project and use it. Do not pull any of Android-specific classes to servlet project. Position.java it's a good example I think - I see no Android dependend classes on first look. – Viacheslav Sep 02 '13 at 17:24
  • @Vyacheslav: line 24 - `Location loc = (Location) data;` - `getData()` does the [dirty job](https://github.com/Utumno/monitoring/blob/master/src/gr/uoa/di/monitoring/android/services/LocationMonitor.java#L336) - no it is not an easy one :) – Mr_and_Mrs_D Sep 02 '13 at 18:49
  • @Mr_and_Mrs_D my bad, I've missed it out. – Viacheslav Sep 02 '13 at 19:20
  • @Mr_and_Mrs_D I've given you my opinion. You are free not to agree with me. Bottom line is, you can't use classes from the android SDK outside of the Android system (common sense, my friend...) so there really isn't much point in this discussion. Good luck! –  Sep 03 '13 at 08:14
  • @YekhezkelYovel: I do not disagree, you miss the point - I do not want to "use classes from the android SDK outside of the Android system (common sense, my friend...)" - the _methods_ the servlet project would use would be the methods in those classes _that are pure java_ ! The problem is _I can't extract a jar from the `MonitoringModel`_ - which (jar) would crash if I would call methods from android.* but work fine as long as I call its pure java methods. Underestimating people's "common sense" won't help you understand what they're after, at least – Mr_and_Mrs_D Sep 03 '13 at 12:02
  • 1
    @Mr_and_Mrs_D, I'm done here. Read your own post and tell me if you asked about making your model an Android project or not. Until you understand your own post no one can't help you. My advise is to rethink everything you need to do or want to do, and rewrite your question. Call me again after you do that. –  Sep 03 '13 at 12:25
  • @Mr_and_Mrs_D here's a quote from your post: "I need the Model to be an Android library project as it contains android classes - but also contains the methods to parse the files in the servlet - is it possible ? How should I set this up ?" Now I arge you to think very carefully before you answer me. –  Sep 03 '13 at 12:27
  • @YekhezkelYovel: Yes I would hope there would be a way - say [extract a jar](http://stackoverflow.com/questions/17063826/how-to-create-jar-for-android-library-project/17064024#17064024) which would have the classes compiled with "stubs" for the android _methods_ but would run fine if I call the non Android ones – Mr_and_Mrs_D Sep 03 '13 at 12:31