Skip to main content

Using Java Web Start to Launch NetBeans

Posted by hansmuller on January 9, 2006 at 4:21 PM PST

About six months ago I had a dream. Not the sort of dream that makes
you wake up shrieking or smiling, and not the kind that brings you
down from the mountain top or even gets you off the couch. Mine was
the kind of dream programmers have. The kind of sloth inspired idea
that comes to you while staring at the screen, wondering if there's
a way to eliminate all of the mouse clicking and key pressing effort
that makes you weary without actually burning calories.

I spend quite a bit of time looking at Java blogs and articles that
incorporate lots of source code. Usually there's a link for zip file
that contains all of the files the document refers to, and maybe a jar
file with a build. Sometimes articles include direct links to source files,
however scanning a pile of source code with the web browser isn't
terribly appealing. The nicest way to look at code and try out APIs,
is to just load everything into a Java IDE like NetBeans. Once that's
done it's possible to use the editor and debugger and all of the other
IDE features to explore the code. Like a real programmer should. And
just like a real programmer, I'm usually too lazy to bother.

So the big idea was to write a web started app that would download a
complete NetBeans project, launch NetBeans, and open the key source
files in the editor. That way, if someone was reading a blog or an
article about some Java project, they could click on a JNLP link - and
with no additional effort! - peruse the code from within the IDE. To
me, this seems like a civilized way to do business. Sadly, I wasn't
able to con one of my colleagues into building such a web started app.
Towards the end of last year, as Sun began to slow down in
anticipation of the Christmas break, I took a crack at building a
NetBeans launcher. You can try it now, by clicking on the handy
launch button below. It's a signed application, because it creates a
temp file and launches a (NetBeans) process on your machine, so you'll
have to click through a security dialog.

To give the example launcher a try, just click the Launch button:


Web started application launch button src="http://weblogs.java.net/blog/hansmuller/archive/jws-launch-button.png"
width="88"
height="23"/>

The launch app depends on the NetBeans OpenFile module to start the
NetBeans IDE, if necessary. Sadly (at the moment) there isn't
an "OpenProject" module, so the best I can do is to show a single
Java source file. If the launch app is unable to locate an installed
copy of NetBeans, it displays a little form that allows choosing
the install directory, or downloading the current NetBeans release.
I haven't tested launch very carefully (works on my machine :-) which
is unwise, since part of the code to find and launch NetBeans is
platform specific. It should work on Windows XP and it might
work on Linux or the Mac or Solaris. I'd be happy to
hear from anyone who's tried it.

The example file downloaded by the launcher, NetBeans.java, deals
with figuring out if and where NetBeans is installed. I didn't
pick this one file out of pride, it's incomplete and contains
some moderately embarrassing hacks. On the other hand, it does
all of the important work. The class is used like this:

NetBeans nb = new NetBeans();
nb.initialize();
if (!nb.isInstalled()) {
    // give the user the opportunity to choose
    // the install directory or download NetBeans
}
else {
    File file = new File("C:\MyProject\src\pkg\MyFile.java");
    try {
        nb.openFile(file);
    }
    catch (NetBeans.Failure e) {
        // report the problem to the user
    }
}

The initialize method uses some shameful heuristics to try and figure
out where NetBeans was installed. Check out the source code from
within the IDE to see what I mean. It would certainly be much nicer
to be able to look in a well known (per platform) place to find out
what versions of NetBeans were installed and where; maybe in the
future that kind of support will emerge.

You can use the launch app on you own web site, just by making a
copy of the JNLP file file
and replace the URL in argument element at the bottom.
The current version links to the example NetBeans.java file:

<argument>http://download.java.net/javadesktop/blogs/hansmuller/launch/NetBeans.java</argument>

You don't need to copy the launch jar files or anything else. Just
create a link to your version of launch.jnlp on your site, and make
sure your web server

is configured to support the JNLP MIME type
.

This was originally intended to be a quick project that I'd finish on
on the long flight home from Prague back in December. It didn't turn
out that way, in part because building (usable) GUIs is always more
work than you'd think. It's also because I took the opportunity to
get introduced to
Matisse.
It's been a long time since I've been comfortable writing Swing GUIs
with a tool. Using Matisse turned out to be pretty inspiring: it
worked well and it greatly simplified the task of evolving a GUI.
Using it was a constant reminder of all of the additional desktop app
building support that should be in NetBeans. More about that in
another blog.

I have a long laundry list of worthy improvements for the NetBeans
launcher. I'd be interested to hear what other developers think of
it and what you-all think should be changed/improved. Here are a
few of TODO items from the top of my list:

  • Download a complete project packed into a jar file and expand
    that into a temp directory. Open a set of project relative source
    files in NetBeans.
  • Log errors, and warnings and provide a way to show them.
  • Show the main window roughly in the middle of the screen.
    And wouldn't it be nice to know which Screen NetBeans was
    already running on - if there were multiple screens.
  • An "OpenProject" NetBeans module. I've been assured that this
    wouldn't be so difficult to write. Making sure that it was
    deployed to the IDE, before the project was launched would be a
    bit of a trick.

If you'd like to look at the complete NetBeans launch project, you'll
find a

zip file of the complete project
here. I realize that it's more
than a little ironic to publish such a link. Hopefully I'll be able to
support loading entire projects (not just files) in round two.

Related Topics >>