• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem creating a database API

 
Jose Lara
Greenhorn
Posts: 23
2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

This is the first time I am working with frameworks from Java, so I have a lot of doubts in general.

I have to create an API of a database able to run both on iOS and Android. I have created databases before using the Collections, but I have never designed APIs before, so this is quite a new world for me.

For this purpose I followed the next tutorial, that explains how to create a RESTfull API using Spark (https://dzone.com/articles/building-simple-restful-api).

I just created my own database, quite more complex than this one and I tested in my localhost using Postman. Until this point everything is working. But my problem begins here, as I am quite lost about all this stuff.

I read that in the case of smartphones, it is necessary to execute the .jar, so I tried to do it first from my laptop but I got the next error: Can't execute jar- file: “no main manifest attribute”.

I extracted the .jar file and checked that the MANIFEST does not include the line "Main-Class: MainClass", so I investigated and found this: http://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute

I added the mentioned lines in my POM file, as it is for Maven, but now I am receiving some new errors when trying to execute the .jar

Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: spark/Request
at com.joselaraproductions.UserController.<init>(UserController.java:10)
at com.joselaraproductions.MainClass.main(MainClass.java:7)
Caused by: java.lang.NoClassDefFoundError: spark/Request
... 2 more
Caused by: java.lang.ClassNotFoundException: spark.Request
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more

Can someone help me? Because I am quite blocked right now and I have been a full day with this.

I also accept some tips, good documentation if you know (with screenshots and so on, not like the official one, that is really complicated for newbies) and whatever thing that could help me with this or future problems.

PS: I use NetBeans in the case that I should add PATHS, change properties or whatever.

Thank you so much for the help.

Best regards.

Jose Lara.
 
Salil Wadnerkar
Ranch Hand
Posts: 77
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you add libraries in Netbeans IDE by using "add libraries", maven does not know about that dependency. So, even though you can build and execute the project using Netbeans, you are having problems running the jar outside the IDE. Since all IDEs support maven projects, I think you should remove all dependencies you added using Netbeans menu and explicitly declare these dependencies in maven's POM. If the project builds using "mvn package", that means you have declared all dependencies to maven correctly.
Now, maven will create the jar correctly. However, it is not an executable jar. So, when you try to run jar using "java -jar <jar-name>", you get the manifest error. So, you should use maven-jar-plugin to create an executable jar. And if you don't want to specify the libraries (spark and other libraries) on the classpath, you should create an executable "uberjar" - so that all dependencies are packed inside one jar.
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.ClassNotFoundException: spark.Request

That looks like the class was specified to be in the spark package. Is the Request.class file in the spark folder in the jar file?
What is in the manifest file for Main-Class?
 
Jose Lara
Greenhorn
Posts: 23
2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
java.lang.ClassNotFoundException: spark.Request

That looks like the class was specified to be in the spark package. Is the Request.class file in the spark folder in the jar file?
What is in the manifest file for Main-Class?


Salil Wadnerkar wrote:When you add libraries in Netbeans IDE by using "add libraries", maven does not know about that dependency. So, even though you can build and execute the project using Netbeans, you are having problems running the jar outside the IDE. Since all IDEs support maven projects, I think you should remove all dependencies you added using Netbeans menu and explicitly declare these dependencies in maven's POM. If the project builds using "mvn package", that means you have declared all dependencies to maven correctly.
Now, maven will create the jar correctly. However, it is not an executable jar. So, when you try to run jar using "java -jar <jar-name>", you get the manifest error. So, you should use maven-jar-plugin to create an executable jar. And if you don't want to specify the libraries (spark and other libraries) on the classpath, you should create an executable "uberjar" - so that all dependencies are packed inside one jar.



Thank you to both of you!

It was exactly as Salil said. I just "installed" Maven in my laptop and was able to build the project using "mvn package"

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.505 s
[INFO] Finished at: 2016-06-06T12:54:35+01:00
[INFO] Final Memory: 19M/142M
[INFO] ------------------------------------------------------------------------
 
Jose Lara
Greenhorn
Posts: 23
2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

One last question and hopefully will be the last one.

Finally I have my database working almost totally fine. The only problem I am having is about saving the information and loading it. For that purpose I am using serialization (or trying at least).

In my RestaurantService file (for the management of the restaurant objects in which is based the database) I have the next two methods for saving and loading the file with the information:



And in my UserController class:



Both classes implements Serialializable, but when I am running I get the next error:



I don't know what is going on. Someone knows what could be happening?

Thank you in advance.

Best regards.

Jose Lara.
 
Salil Wadnerkar
Ranch Hand
Posts: 77
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure your Restaurant class implements Serializable interface. Also, make sure, the classes that Restaurant class uses in "HAS A" relationship (types of instance variables of Restaurant) implement Serializable interface. In case, some of these instance variables don't need to be serialized (that is, you are OK with them initialized to null when the Restaurant object is deserialized), declare these instance variables as "transient"
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.joselaraproductions.Restaurant

Does the com.joselaraproductions.Restaurant class implement Serializable?

See the API doc for that error message. It's a class that is doumented in the API doc:

public class NotSerializableException
extends ObjectStreamException
Thrown when an instance is required to have a Serializable interface. The serialization runtime or the class of the instance can throw this exception. The argument should be the name of the class.
 
Jose Lara
Greenhorn
Posts: 23
2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salil Wadnerkar wrote:...


Norm Radder wrote:...


Yes, all the classes implements Serializable, so I don't understand from where this error is coming =/
 
Salil Wadnerkar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code for com.joselaraproductions.Restaurant ?
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you make a small, simple program that compiles, executes and shows the problem?
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose Lara wrote:Yes, all the classes implements Serializable, so I don't understand from where this error is coming =/

As Salil suggested -- have you checked that the fields which are instances of other classes are also Serializable (or marked as transient)?

Do you have any inner classes?
 
Jose Lara
Greenhorn
Posts: 23
2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:
Jose Lara wrote:Yes, all the classes implements Serializable, so I don't understand from where this error is coming =/

As Salil suggested -- have you checked that the fields which are instances of other classes are also Serializable (or marked as transient)?

Do you have any inner classes?


No, I don't have any inner class.

I am not sure which ones I should mark as transient =/ I post the code of the Restaurant class for Salil, let me know if I should post of another class.


Salil Wadnerkar wrote:Can you post the code for com.joselaraproductions.Restaurant ?


Sure!




Norm Radder wrote:Can you make a small, simple program that compiles, executes and shows the problem?


Sure! I will try it and post it here. That's a good idea to make it simpler!


THANKS TO EVERYONE! YOU ALL ARE THE BEST!
 
Salil Wadnerkar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The offending instance variable may be: private ArrayList<Menu> menus;
Is your Menu class serializable?
 
Jose Lara
Greenhorn
Posts: 23
2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salil Wadnerkar wrote:The offending instance variable may be: private ArrayList<Menu> menus;
Is your Menu class serializable?


Yes, same thing:



I just eliminated the System.exit(0) from the exceptions and even if I am getting the same issue, I think that is working fine =/

Anyway, I would like to solve it in order to work properly without throwing exceptions!

Thanks again!
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well .. I just tried your code and I didn't get a java.io.NotSerializableException. I did however get a java.io.EOFException, which is expected because your code treats EOF as an error, rather than an indication that there is nothing more to be read. readObject only returns a null if the obecj read was a null, it does not indicate end of objects.

I added a println so that I could see each object as it was being read.


Name: A&W/lnCity: Richmond/lnPostcode: V6Y3C6/lnAddress: 12203 Garden City Road/ln
Not possible to add the Restaurant
java.io.EOFException
   at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
   at java.io.ObjectInputStream.readObject0(Unknown Source)
   at java.io.ObjectInputStream.readObject(Unknown Source)
   at com.joselaraproductions.RestaurantService.loadRestaurantList(RestaurantService.java:87)
   at com.joselaraproductions.UserController.<init>(UserController.java:7)
   at com.joselaraproductions.Tester.main(Tester.java:16)
 
Jose Lara
Greenhorn
Posts: 23
2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:Well .. I just tried your code and I didn't get a java.io.NotSerializableException. I did however get a java.io.EOFException, which is expected because your code treats EOF as an error, rather than an indication that there is nothing more to be read. readObject only returns a null if the obecj read was a null, it does not indicate end of objects.

I added a println so that I could see each object as it was being read.


Name: A&W/lnCity: Richmond/lnPostcode: V6Y3C6/lnAddress: 12203 Garden City Road/ln
Not possible to add the Restaurant
java.io.EOFException
   at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
   at java.io.ObjectInputStream.readObject0(Unknown Source)
   at java.io.ObjectInputStream.readObject(Unknown Source)
   at com.joselaraproductions.RestaurantService.loadRestaurantList(RestaurantService.java:87)
   at com.joselaraproductions.UserController.<init>(UserController.java:7)
   at com.joselaraproductions.Tester.main(Tester.java:16)


Thanks Ron! I am going to double-check the code!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!