Tag Archives: java

Java – Appending objects to binary files

Java is tricky, you cannot add objects to already existent files with default classes.

Here is the solution:

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class Test{
    private static String filename = "test";

    public static void main(String[] args) {
        writeToBinary (filename, "a", true);
        writeToBinary (filename, "b", true);
        writeToBinary (filename, "c", true);
        readFromBinaryFile (filename);

    public static void writeToBinary (String filename, Object obj, boolean append){
        File file = new File (filename);
        ObjectOutputStream out = null;

            if (!file.exists () || !append) out = new ObjectOutputStream (new FileOutputStream (filename));
            else out = new AppendableObjectOutputStream (new FileOutputStream (filename, append));
            out.flush ();
        }catch (Exception e){
            e.printStackTrace ();
                if (out != null) out.close ();
            }catch (Exception e){
                e.printStackTrace ();

    public static void readFromBinaryFile (String filename){
        File file = new File (filename);

        if (file.exists ()){
            ObjectInputStream ois = null;
                ois = new ObjectInputStream (new FileInputStream (filename));
                while (true){
                    String s = (String)ois.readObject ();
                    System.out.println (s);
            }catch (EOFException e){

            }catch (Exception e){
                e.printStackTrace ();
                    if (ois != null) ois.close();
                }catch (IOException e){
                    e.printStackTrace ();

    private static class AppendableObjectOutputStream extends ObjectOutputStream {
          public AppendableObjectOutputStream(OutputStream out) throws IOException {

          protected void writeStreamHeader() throws IOException {}





Netbeans – “not all requested modules can be enabled”

If you have installed Oracle Java 8 via default installers and you get the awkward message “not all requested modules can be enabled” in your Linux box when trying to create a new java project in Netbeans you should:

  1. open netbeans.conf
    1. set netbeans_jdkhome to point to the proper location, eg:


This file should be found in install dir of netbeans

The easiest way to find netbeans.conf is to run the following command:

  • find / -name netbeans.conf 2> /dev/null

In default installation it should be something like:

  • /home//netbeans-X.Y/etc/netbeans.conf

Install & Uninstall Java

Install Java

NOTE: if you want to develop to Android then:

  1. install an 64bit version
  2. install Oracle Java as shown below

You can use 32bit but it does not worth it.


If you want the default (OpenJava) then:

  1. sudo apt-get update
  2. sudo apt-get install default-jre
  3. sudo apt-get install default-jdk

Oracle Java

But preferably if you are going to develop in Java, install Sun version instead:

  1. sudo apt-get install python-software-properties
  2. sudo add-apt-repository ppa:webupd8team/java
  3. sudo apt-get update
  4. sudo apt-get install oracle-java7-installerย ย ย ย ย ย ย ย ย ย ย  (or 8 etc)
  5. sudo update-alternatives –config java (to select default Java)

If you want to setup JAVA_HOME:

  1. sudo update-alternatives –config java
  2. From the menu above, write down the path of the version you want (eg /usr/lib/jvm/java-7-oracle)
  3. edit /etc/environment (vim, nano etc)
  4. add or update the line: JAVA_HOME=”YOUR_PATH”
  5. reload environment: source environment

Uninstall Java

If you want to completely uninstall java then:

  1. Remove packages:
dpkg-query -W -f='${binary:Package}\n' | grep -E -e '^(ia32-)?(sun|oracle)-java' -e '^openjdk-' -e '^icedtea' -e '^(default|gcj)-j(re|dk)' -e '^gcj-(.*)-j(re|dk)' -e '^java-common' | xargs sudo apt-get -y remove
sudo apt-get -y autoremove
  1. Purge config files:
dpkg -l | grep ^rc | awk '{print($2)}' | xargs sudo apt-get -y purge
  1. Remove Java config and cache directory:
sudo bash -c 'ls -d /home/*/.java' | xargs sudo rm -rf
  1. Remove manually installed JVMs:
sudo rm -rf /usr/lib/jvm/*
  1. Remove Java entries, if there is still any, from the alternatives:
for g in ControlPanel java java_vm javaws jcontrol jexec keytool mozilla-javaplugin.so orbd pack200 policytool rmid rmiregistry servertool tnameserv unpack200 appletviewer apt extcheck HtmlConverter idlj jar jarsigner javac javadoc javah javap jconsole jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic schemagen serialver wsgen wsimport xjc xulrunner-1.9-javaplugin.so; do sudo update-alternatives --remove-all $g;
  1. Search for possible remaining Java directories:
sudo updatedb
sudo locate -b '\pack200'

And look for entries with jre or jdk and delete them

At the sidebar menu you can find links to scripts that do all those steps at once.