Netbeans Platform parsing command line arguments

August 1st, 2007 | by Tonny Kohar |

New features in Netbeans Platform 6 is the ability to parsing command line, so let’s try to parse command line to automatically open document on startup. This example below are using Sketsa SVG Editor as an example

  • First create an object that inherit from OptionProcessor
    public class SketsaOptionProcessor extends OptionProcessor {
        private Option openOption = Option.defaultArguments();
        private Option openOption2 = Option.additionalArguments(
                                                    'o', "open");
        public Set getOptions() {
            HashSet set = new HashSet();
            return set;
        public void process(Env env, Map values) 
                            throws CommandException {
            List<string> filenameList = new ArrayList<string>();
            Object obj = values.get(openOption);
            if (obj != null) {
                filenameList.addAll(Arrays.asList((String[]) obj));
            obj = values.get(openOption2);
            if (obj != null) {
                filenameList.addAll(Arrays.asList((String[]) obj));
            for (int i = 0; i &lt; filenameList.size(); i++) {
                File file = new File(filenameList.get(i));
                if (!file.isAbsolute()) {
                    file = new File(env.getCurrentDirectory(),
                try {
                    DataObject data = DataObject.find(
                    OpenCookie cookie = data.getCookie(OpenCookie.class);
                } catch (OutOfMemoryError ex) {
                    String msg = Application.getMessage(
                    NotifyDescriptor nd = new NotifyDescriptor.Message(
                } catch (Exception ex) {
                    NotifyDescriptor nd = new NotifyDescriptor.Message(

    the important here is the 2 overidden methods: getOptions() and process(Env,Map) the getOptions indicate which command line you want to capture, in this case we want to capture defaultArgument and –open some_file or -o some_file. So the following will works

    sketsa artwork.svg
    sketsa --open artwork.svg
    sketsa -o artwork.svg
  • Since the OptionProcessor is a service or lookup, so we need to register it somewhere. We register it in META-INF services by
    - create META-INF/services folder in the source folder. 
    - put the following file with a name
    - inside that file put the text 
      on the first line.

    This indicate that org.netbeans.spi.sendopts.OptionProcessor service is implemented in kiyut.sketsa.SketsaOptionProcessor

  • compile and build the project. Now the NB Platform based application able to parse command line argument
  • Note: this command line argument processing need Netbeans 6

    Tags: , ,

You must be logged in to post a comment.