Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; public class Main { public static byte[] objectToByte(Object obj) throws Exception { ObjectOutputStream oos = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(obj); return bos.toByteArray(); } finally { if (oos != null) oos.close(); } } }