Tuesday, November 12, 2013

Install Eclipse on Ubuntu 12.10 64bit

This article describes process about:
- installation of JVM;
- installation of Eclipse IDE;
- tunning the Eclipse IDE for starting  from launch panel.

Installation of JDK.

  1.  Go to Java SE Downloads page and download archive .tar.gz with newest version of JDK - jdk-XuXX-linux-x64.tar.gz.
  2. Go to Eclipse Download page and downdload Eclipse IDE for Java EE Developers for Linux 64 bit.
  3. Suggest that download catalog is ~/Download.
  4. Go to ~/Download

  5. cd /Download

    and unpacking our JDK - jdk-XuXX-linux-x64.tar.gz.

    tar xvfz JDK - jdk-XuXX-linux-x64.tar.gz
    
  6. move the JDK 7 directory to /usr/lib
    sudo mkdir -p /usr/lib/jvm
    sudo mv ./jdk.1.7.0_02 /usr/lib/jvm/jdk1.7.0
  7. Now run
    sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1
    sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1
    sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1
  8. Correct the file ownership and the permissions of the executables:
    sudo chmod a+x /usr/bin/java sudo chmod a+x /usr/bin/javac
    sudo chmod a+x /usr/bin/javaws
    sudo chown -R root:root /usr/lib/jvm/jdk1.7.0
  9. Run
    sudo update-alternatives --config java
  10. If you have a problem with maven like this

    Missing artifact com.sun:tools:jar:1.5.0:system pom.xml

    You can have eclipse start up using your built in JDK by altering the eclipse.ini and adding something like

    -vm
    /usr/java/jdk1.7.0_17/bin/java

    The example of eclipse.ini in Linux:

    -startup
    plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
    --launcher.library
    plugins/org.eclipse.equinox.launcher.gtk.linux.x86_1.1.200.v20120913-144807
    -product
    org.eclipse.epp.package.jee.product
    --launcher.defaultAction
    openFile
    -showsplash
    org.eclipse.platform
    --launcher.XXMaxPermSize
    256m
    --launcher.defaultAction
    openFile
    -vm
    /usr/java/jdk1.7.0_17/bin/java
    -vmargs
    -Dosgi.requiredJavaVersion=1.5
    -Dorg.eclipse.swt.browser.DefaultType=webkit
    -Dhelp.lucene.tokenizer=standard
    -XX:MaxPermSize=256m
    -Xms40m
    -Xmx512m

    From stackoverflow.com
  11. If you have problem like this:

    Unhandled event loop exception No more handles [Unknown Mozilla path (MOZILLA_FIVE_HOME not set)]

    I founded the solution here askubuntu

    Use Webkit instead of XULRunner
    Install the package  and configured Eclipse  to use Webkit instead of Xulrunner by adding this line to the eclipse.ini:

    -Dorg.eclipse.swt.browser.DefaultType=webkit

    Restart Eclipse.
  12. If you have problem like this:

    Event log:

    Unhandled event loop exception


    org.eclipse.swt.SWTError: No more handles
    at org.eclipse.swt.SWT.error(SWT.java:4445)
    at org.eclipse.swt.SWT.error(SWT.java:4334)
    at org.eclipse.swt.SWT.error(SWT.java:4305)
    at org.eclipse.swt.browser.Browser.<init>(Browser.java:103)

    Just:
    sudo apt-get install libwebkitgtk-1.0-0



Saturday, September 14, 2013

Work with org.apache.commons.configuration.PropertiesConfiguration

When standard tools (java.util.prefs.Preferences) not enough for solving your goals with saving some properties, org.apache.commons.configuration.PropertiesConfiguration can help you.

  1. Configuration file creation.

    private static final Logger LOGGER = LoggerFactory.getLogger(ClientSettings.class)
    String propertyPath = "/userdir";
    String fileName = "fileName.property";
    File propertyDir = new File(propertyPath);
    if(!propertyDir.exists()) {
        if (!propertyDir.mkdirs()) {
                    throw new IllegalStateException(String.format("Cannot create property folder: %s", propertyDir.getAbsoluteFile()));
        }
    }
          File propertiesFile = new File(propertyDir, fileName);
          if (!propertiesFile.exists()) {
              try {
                 if (!propertiesFile.createNewFile())
                    throw new IllegalStateException(String.format("Cannot create property file: %s", propertiesFile.getAbsoluteFile()));
              } catch (IOException e) {
                LOGGER.error("Cannot create configuration file: %s", e);
              }
           }

  1. Specify the location of the file this configuration is based on and load the configuration.

    try {
           //allows to specify the configuration source as a file
           configuration.setFile(propertiesFile);
           //Locate the specified file and load the configuration.
           configuration.load();
           //Save the configuration to the specified file.
           configuration.save(propertiesFile);
    } catch (ConfigurationException e) {
           LOGGER.error("Cannot initializing a Configuration object.",e);
           return null;
    }


    The following possibilities exist:
    • URLs: With the method setURL() a full URL to the configuration source can be specified. This is the most flexible way. Note that the save() methods support only file: URLs.
    • Files: The setFile() method allows to specify the configuration source as a file. This can be either a relative or an absolute file. In the former case the file is resolved based on the current directory.
    • As file paths in string form: With the setPath() method a full path to a configuration file can be provided as a string.
    • Separated as base path and file name: This is the native form in which the location is stored. The base path is a string defining either a local directory or a URL. It can be set using the setBasePath() method. The file name, non surprisingly, defines the name of the configuration file.
      The configuration source to be loaded can be specified using one of the methods described above. Then the parameterless 
      load() method can be called. 
  1. Get String properties.

    configuration.getString(String key);
  2. Set properties.
    configuration.setProperty(String key, Object value);
    configuration.save();

    If property with such key is exist, value will be overridden.
  3. Add new property.

    configuration.addProperty(String key, Object value);configuration.save();

    If property with such key is exist, new property will be add anyway.

Monday, September 9, 2013

Install Oracle Java 8 in Ubuntu 12.04.

Run in terminal simple comands:
  1. Update repositories and install Java:

    sudo add-apt-repository ppa:webupd8team/java
    sudo apt-get update && sudo apt-get install oracle-java8-installer
  2. Check java version:

    java -version

    Output must be similar to:

    java version "1.8.0-ea"
    Java(TM) SE Runtime Environment (build 1.8.0-ea-b54)
    Java HotSpot(TM) Server VM (build 24.0-b21, mixed mode)
  3. If you have several version of Java, you can switch between them:

    Check all installed JVM  on your computer:
    sudo update-java-alternatives -l

    Choose JVM and switch:
    sudo update-java-alternatives -s java-8-oracle

Convertion Russian characters to one with Unicode-encoded characters for Java application


  1.  Online http://native2ascii.net .
  2. With utillity natime2ascii in Sun JDK.

    Documentation: http://docs.oracle.com

    Create a file:
    touch ~/Test.txt

    Write to file Russian characters:
    echo Русский > Test.txt

    Go to the Java jdk bin directory:
    cd 
    /usr/lib/jvm/jdk1.7.9/bin

    And run the natime2ascii:
    ./native2ascii -encoding UTF-8 ~/Test.txt ~/Test.txt

    Look in the file:
    less ~/Test.txt
     \u0420\u0443\u0441\u0441\u043a\u0438\u0439


Tuesday, May 7, 2013

Replacing the default Exception dialog in Netbeans RCP

If you building a Netbeans RCP application and you would like to replace the current Exception dialog by a customised one. There are two ways to replace the standard “unexpected exception” window:

  1. The Lookup supports extension to the JDK's standard. It allows a module to remove class registered by another one.
    1. Implement your own java.util.logging handler  (using "@ServiceProvider(service=Handler.class)"). For example my.company.MyErrorHandler.
    2. Adding a META-INF/services for java.util.logging.Handler in your source. 
    3. Add to this file:

      #-org.netbeans.core.NbErrorManager
      my.company.MyErrorHandler

      The reason why the removal line starts with #- is to keep compatibility with JDK's implementation. The # means comment and thus JDK will not interpret the line and will not get confused by the - before class name.

      bits.netbeans.org
  2. You can superseding NbErrorManager.
    1. Implement your own java.util.logging handler 
    2. Use:

      @ServiceProvider(service=Handler.class, supersedes ={"org.netbeans.core.NbErrorManager"})")

      Geertjan's Blog