November 9, 2014

Implementing Derby in Greenfoot’s Jar Deployment: Tetris Game

Hello, this November should be called database month in my blog! I’ll post databases related technology such as MySQL, Derby, etc (from {Java} developer’s point of view) to familiarize myself with such technology so I can learn more advanced technology, such as spatial database technology.

Simple greenfoot tetris game
While there are so many libraries out there on the internet that you can use within greenfoot, this post will be the implementation of embedded derby in your greenfoot scenario deployment and a little of its problem. A little knowledge about greenfoot (2.4.0), jar packaging, and embedded derby will be helpful when reading this article. I also post my tetris game distribution which you can download below this post.



Imagine a situation where you have developed from scratch, a tetris game in greenfoot, and then you said to yourself,

“Hmm, I’m too lazy to write down the score saving code manually, I should use derby instead.“

If you want to utilize derby or any other libraries in greenfoot IDE, the IDE has to know where the derby library is and load the derby library’s driver from there. For that, you need to do:

Set the classpath in greenfoot environment by:
  1. Clicking edit -> preferences.
  2. In libraries tab, in user libraries, click add and enter the location to your derby.jar files .
  3. Finally, restart greenfoot.
Then, you can connect to the database via JDBC through derby’s driver. The path to the database will begin from your greenfoot scenario’s root folder.

Problem with Deployment

While it is relatively easy to use derby to create and access database right away in your greenfoot scenario, it’s not the same if you deploy it into a jar file.

You see me losing the game..
If you deploy (or ‘export’) your scenario and run it as is, all of your database related code will be useless. I don’t know the exact problem, but what I do know is that I’ve never saw any ‘jar in a jar’ files in any Java application’s jar. Maybe, your game can’t access the jar files when it is a jar file by itself and thus, the driver will never loaded.

But, worry not--remembering the Java packaging/deployment tutorials, I’ve decided to put derby library (jar) in the same directory as the game’s jar file (not inside the jar) and set the classpath in the game’s jar manifest file to include the derby library.

Derby’s database system and your database folder, will belong in the directory (root directory) that contain your game and derby library’s jar files.

These are the highscore which is saved using derby library
Finally, besides your game’s jar file and the derby’s library, you should put the (pre-generated) database in your game distribution.

The final file structure of your distribution more or less should look like this:

[your game directories] /
   |--- [your game].jar
   |--- derby.jar
   |---derby.log
   |---database /
     |---[your database]

To summarize things up, you can use external libraries in greenfoot by addressing the libraries in the preferences. After you deploy your greenfoot scenario into jar file, you should put the library besides your scenario jar’s file (or database folder) and tell your game’s jar file manifest where the library is.

Tetris game: https://drive.google.com/file/d/0B27ziWaCDimLenNKNkZVYWNwdms/view?usp=sharing


1 comment:

  1. Shit man, I thought you already gave up on these shits. You should use a responsive template, yours kinda outdated, you know that for sure.

    ReplyDelete