Jun 08

How to install zeromq (0MQ) in Ubuntu with Java bindings

I tested this in Ubuntu 13.04, but essentially this should work in any Ubuntu distribution. First install the libtool, autoconf, automake, uuid-dev, e2fsprogs packages.

sudo apt-get install libtool autoconf automake uuid-dev e2fsprogs

Then clone the 0mq source using,

git clone git://github.com/zeromq/libzmq.git

Then go in to the libzmq directory and run the following commands in order.

./autogen.sh
./configure
make
sudo make install
ldconfig -v

Then run the following command and check the output.

ls -al /usr/local/lib/libzmq.*

The output should be similar to,

-rw-r--r-- 1 root root 7082022 ජූනි 7 14:16 /usr/local/lib/libzmq.a
-rwxr-xr-x 1 root root 947 ජූනි 7 14:16 /usr/local/lib/libzmq.la
lrwxrwxrwx 1 root root 15 ජූනි 7 14:16 /usr/local/lib/libzmq.so -> libzmq.so.3.0.0
lrwxrwxrwx 1 root root 15 ජූනි 7 14:16 /usr/local/lib/libzmq.so.3 -> libzmq.so.3.0.0
-rwxr-xr-x 1 root root 2876918 ජූනි 7 14:16 /usr/local/lib/libzmq.so.3.0.0

Now you should install the Java bindings for 0mq (in order to use omq from Java of course!). First check whether the JAVA_HOME environment variable is correctly set using,

echo $JAVA_HOME

This should output the location that you have installed Java. If it’s giving an empty output, then set it manually using

export JAVA_HOME=/location/to/your/java/installation

In my case the command is,

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_17

Then clone the java bindings for 0mq using,

git clone https://github.com/zeromq/jzmq.git

now go inside the jzmq folder. Now same as before, run the following commands in order.

./autogen.sh
./configure
make
sudo make install

Now run the following command to verify the installation.

ls -al /usr/local/lib/*jzmq* /usr/local/share/java/*zmq*

The output should be like the following.

-rw-r--r-- 1 root root 535266 ජූනි 7 14:40 /usr/local/lib/libjzmq.a
-rwxr-xr-x 1 root root 998 ජූනි 7 14:40 /usr/local/lib/libjzmq.la
lrwxrwxrwx 1 root root 16 ජූනි 7 14:40 /usr/local/lib/libjzmq.so -> libjzmq.so.0.0.0
lrwxrwxrwx 1 root root 16 ජූනි 7 14:40 /usr/local/lib/libjzmq.so.0 -> libjzmq.so.0.0.0
-rwxr-xr-x 1 root root 242784 ජූනි 7 14:40 /usr/local/lib/libjzmq.so.0.0.0
-rw-r--r-- 1 root root 40618 ජූනි 7 14:40 /usr/local/share/java/zmq.jar

Notice the last line, that’s the newly created jar that connects to 0mq. Now you can use 0mq inside a Java programme.

Troubleshooting

If you get any of the following errors (which I got) when building the jar, export the mentioned system variables in the terminal to resolve them.

error

checking for javah… no

configure: error: cannot find javah

solution

export JAVAH=/location/to/your/java/installation/bin/javah

ex – export JAVAH=/usr/lib/jvm/jdk1.7.0_17/bin/javah

error

checking for jar… no

configure: error: cannot find jar

solution

export JAR=/location/to/your/java/installation/bin/jar

ex – export JAR=/usr/lib/jvm/jdk1.7.0_17/bin/jar

(Most of the content of this guide is based up on https://github.com/mslinn/zeromq-demo#ubuntu )

Share Button
Jul 30

Java Slick sound problems in Linux

My operating system is Ubuntu Linux 11.04 and I am using Java Slick library to build a game in these days. I faced the following error when I tried to load sounds using Music backMus=new Music(“path/to/file”); .

INFO:Initialising sounds..AL lib: oss.c:179: Could not open /dev/dsp: No such file or directoryERROR:Sound initialisation failure.ERROR:Could not locate OpenAL library. org.lwjgl.LWJGLException: Could not locate OpenAL library.

Searched everywhere for hours to find a solution for this and tried many guides which were written to solve this problem, but none of them were a success. Finally I got to know that this error is caused because Slick library uses a very old version of LWJGL (Light Weight Java Gaming Library). If you too are having this same problem use the following guidelines to rectify it.

  • Download the latest version of LWJGL from http://lwjgl.org/download.php.(Download the file named lwjgl-x.x.x.zip).
  • Extract the downloaded file.
  • Copy the libraries that you are using from the jar folder in the extracted directory to your games Slick library folder. (Confirm file overwrites).
  • Copy the relevant native files from the native folder to your games Slick natives folder. (Confirm file overwrites).
  • Now reload the libraries in your IDE-if you are using any (For an example,in Net Beans remove all the slick libraries and add them again).

Now the game will play the sounds smoothly…!

 

Share Button
Jan 24

Handling images in java using imageIO

Have you ever faced the need of sending an image through a network using a Java program? If yes, you must have probably read the image to a byte array sent it through the network. The problem with this method is it is too long and you have to remake the image file ( using the received byte array) at the destination to take any use of it.

But in this article I’m going to show how to read an image to a Java class called “Image” and use it appropriately. You can use this “Image” class directly in GUI’s, send it through networks, store it, etc. Sol let’s look at the code now. The code consists of two parts. Reading the image to an instance of the “Image” class and writing it back to the hard drive. I have put comments in almost every step. So understanding the code should be easy.

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class Main {

 public void copyImage(){

 //path of the image to be read
 String path="picture.jpg";

 //create a file entry in java for that image
 File imageSrc=new File(path);

 if(!imageSrc.exists()){
 System.out.println("Input image does not exist.");
 System.exit(0);
 }

 try {

 /*
 * This part of the code is for reading the image in to
 * an instance of an Image class.
 */

 //Create a stream to read the image
 ImageInputStream iis = ImageIO.createImageInputStream(imageSrc);
 /*
 * ImageIO is a class containing static convenience methods for locating
 *ImageReaders and ImageWriters, and performing simple encoding and decoding.
 */

 //get a collection of Image readers which support jpg
 Iterator<?> imgReaders = ImageIO.getImageReadersByFormatName("jpg");

 //get the first image reader from the collection
 ImageReader reader = (ImageReader) imgReaders.next();

 //set the input for the image reader
 reader.setInput(iis, true);
 /*
 * Setting seekForwardOnly parameter ensures that the image file is
 * read in the ascending order.
 *
 */

 //to ensure that the default read parameters are used when
 //decoding data from the stream
 ImageReadParam param = reader.getDefaultReadParam();

 //read the image
 Image image = reader.read(0, param);

 /*
 *
 * This part of the code is for writing the image object
 * back to the hard disk as an image file.
 */

 //To render the image
 BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);

 //using "painter" we can draw in to "bufferedImage"
 Graphics2D painter = bufferedImage.createGraphics();

 //draw the "image" to the "bufferedImage"
 painter.drawImage(image, null, null);

 //the new image file
 File outputImg=new File("output.jpg");

 //write the image to the file
 ImageIO.write(bufferedImage, "jpg", outputImg);

 System.out.println("Image transfer was successful...!");

 } catch (IOException ex) {
 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
 }
 }

 public static void main(String[] args) {
 // TODO code application logic here

 Main main=new Main();
 main.copyImage();
 }

}

You can download the text file from here.

As you must have understood, I have written the code to read a JPEG image. I tested the same code for BMP, PNG, and TIFF. All those formats worked just fine. The only draw back that I saw was there is a quality loss when using JPEG. To change the image format, change the following code segments (change xxx to the desired foramt).

//path of the image to be read

String path="picture.xxx";

————————————————————————————————————————————————————————–

//get a collection of Image readers which support jpg
 Iterator<?> imgReaders = ImageIO.getImageReadersByFormatName("xxx");

————————————————————————————————————————————————————————–

//the new image file
File outputImg=new File("output.xxx");

————————————————————————————————————————————————————————–

//write the image to the file
ImageIO.write(bufferedImage, "bmp", outputImg);

————————————————————————————————————————————————————————–

If you used a byte array to read the image file, you have to make an image file( on the hard drive) before you use that image again. But using the above method you can directly use it in the Java program. Click here to see an example on that.

As you can see, the possibilities are endless. So experiment and enjoy…..!

Share Button