Saturday, October 6, 2007

run netbeans directly from a jar

To launch NetBeans from a jar is useful when you want to do something different, or, if you hate the native launcher and want to make Java a really cross platform one. Hansmuller has wrote a blog about Using Java Web Start to Launch NetBeans. I tried it but failed to locate the NetBeans homedir, and there seems to be no source attached. So I googled for a while and find this page suggesting how NetBeans start itself. But the most useful information comes from the following article, which is conspicuously, from Sun's site.

Generic Launcher

The $nbplatform cluster is guaranteed to contain $nbplatform/lib/nbexec executable that is supposed to be used by final products. Example:

The NetBeans IDE product should provide its own executable /usr/bin/netbeans which will locate the $nbplatform/lib/nbexec (currently implemented here for unix and here for windows) executable, read their own configuration files, find out locations of additional clusters and pass --clusters /pathto/nb4.0:/pathto/ide4:/pathto/enterprise7 and --userdir $HOME/.myproductuserdir to it.

The Native Product should have its own launcher called netbeans-native and read its own settings, specify its own user directory and list of clusters (probably --clusters /pathto/ide6:/pathto/web9 as the Native product is extension of the base NetBeans IDE without the enterprise part) and pass that information to the generic $nbplatform/lib/nbexec.

Configuring the Launcher

The behaviour of nbexec launcher can be influenced by special arguments and environment variables:
  • --clusters - specifies the paths to clusters that shall be placed on the execution stack (config, modules, configurations, etc.). Individual elements are separated using java.io.File.pathSeparator so for example on Windows it is ; and on Unix :

  • --branding - gives the name of branding that shall be used

  • regular parameters - these are end user oriented parameters printed when invoked with --help option:
    Usage: bin/../platform4/lib/nbexec {options} arguments

    General options:
    --help show this help
    --jdkhome path to Java(TM) 2 SDK, Standard Edition
    -J pass to JVM

    --cp:p prepend to classpath
    --cp:a append to classpath

    Core options:
    --laf use given LookAndFeel class instead of the default
    --fontsize set the base font size of the user interface, in points
    --locale use specified locale
    --userdir use specified directory to store user settings


Now we are assured that NetBeans can and is in deed launched from a jar(or a cluster of jars ^_^), and the .exe thing is just a configuration reader. Look into the nbexec file, and you will finally get the command like this:
java -Djdk.home=/usr/lib/jvm/java-6-sun -classpath $nbhome/platform6/lib/boot.jar:$nbhome/platform6/lib/org-openide-modules.jar:$nbhome/platform6/lib/org-openide-util.jar:$nbhome/platform6/lib/locale/boot_ja.jar:$nbhome/platform6/lib/locale/boot_pt_BR.jar:$nbhome/platform6/lib/locale/boot_zh_CN.jar:$nbhome/platform6/lib/locale/org-openide-modules_ja.jar:$nbhome/platform6/lib/locale/org-openide-modules_pt_BR.jar:$nbhome/platform6/lib/locale/org-openide-modules_zh_CN.jar:$nbhome/platform6/lib/locale/org-openide-util_ja.jar:$nbhome/platform6/lib/locale/org-openide-util_pt_BR.jar:$nbhome/platform6/lib/locale/org-openide-util_zh_CN.jar:$nbhome/platform6/lib/locale/swing-l10n_pt_BR.jar:/usr/lib/jvm/java-6-sun/lib/dt.jar:/usr/lib/jvm/java-6-sun/lib/tools.jar -Dnetbeans.dirs=$nbhome/nb5.5:$nbhome/ide7:$nbhome/enterprise3/ -Dnetbeans.home=$nbhome/platform6 org.netbeans.Main --userdir /home/yang/.netbeans/
BTW, NetBeans can be shared between linux and M$win.