Why NetBeans is awesome. Part 2: Intelligent Java code completion

Welcome to another part of “Why NetBeans is awesome” series. What I’d like to show you today is how does NetBeans smart code completion work. Let’s look at this simple example:
Continue reading…


InfoShare 2011 conference

Been on technical track of InfoShare conference last Friday. Mostly to attend Adam Bien presentation and workshop. All the conference talks were great, but what I need to say is that Adam presentations are something you can’t miss. 100% Java and no marketing bullshit as someone said :). It was just that. No slides, no off topic, just code. You could ask whatever you were interested in regarding J2EE and Adam was ready to answer. Not on next Friday, but here and now. He’s a real pro and if you have an opportunity to meet him on some workshop, then you really have to go. I was driving for 5 hours for this conference and it was worth it.
Continue reading…

Why NetBeans is awesome. Part 1: Swing form editor and custom components.

I’m great fan of NetBeans IDE and think it really doesn’t get enough noise. So I’m starting my Why NetBeans is awesome series of posts that show some NetBeans features you might not know about, but are really cool or makes life really easier.

First in the series is about great Java Swing form editor and how you can use it with custom components you have created in your application. What NetBeans allows you to do is to just add your components to palette and drag’n’drop it anywhere you need it. What’s coolest there is that it will look exactly like you designed it and will allow you to operate on properties (via getter/setter you have in your component) using Properties table you’re used to while working with ootb Swing components. Here’s how…

Continue reading…

Netbeans source files license header by project

In case you’ve never found this Geertjan’s post Project-Level License Settings in NetBeans IDE 6.0. There is some extreamly useful project specific license feature in Netbeans that really makes life easier when you swap projects often, and each is made for different customer with different license. Here’s what you have to do to make Netbeans fill source files header for you:
Continue reading…

Java 1.5 annotation processors and Netbeans

Netbeans has great support for annotation processors in editor, but it’s only available if you’re developing Java 1.6 application. But because Netbeans is using standard Ant scripts as it’s build system you can easily integrate 1.5 annotation processors with it. What you have to do is edit project build.xml file and add new apt target:
Continue reading…

Don’t use Glassfish 3.1 with Netbeans 6.9.1

I’ve recently been notified by Glassfish update tool (one that is popping up in tray if you use Windows or that can be accessed from server admin console) that there are several updates available. Just hit OK and what you get is 3.0.1 to 3.1 update and no Netbeans integration. It’s even unable to start server so don’t even try to deploy anything.

So if you rely on Glassfish integration in Netbeans you have two options: Don’t do any updates for Glassfish (and don’t install any new modules, they are all in version 3.1 now) or switch to Netbeans 7.0 beta.

The good thing is that you only need to download old 3.0.1 from glassfish.java.net and unpack it somewhere to get back to work.

Pity that they haven’t done an update for stable Netbeans for latest Glassfish or some notification on server update, but 7.0 is so near (it should be ready in month or so, just check for yourself: Netbeans roadmap) that I doubt there will be any more patches for 6.9.

I’ll stick with Glassfish 3.0.1 till then…

Netbeans 7.0 is released and Glassfish 3.1 integration works seamlessly again. It’s really great.

How to write your source provider for class in Netbeans

I’ve been recently writing plugin for Netbeans IDE that should return source from some custom place when you do Ctrl+click or Go-to type for class that source could not be found.

It took me some time to find this out, but stuff is pretty easy in 6.8 and above.

  • First you need to implement BinaryElementOpen and declare service provider for BinaryElementOpen.class.
    It’s quite simple if you use annotations, no ugly layer.xml needed:):

    @org.openide.util.lookup.ServiceProvider(service = org.netbeans.modules.java.BinaryElementOpen.class, position = 20)
    public class GrokElementOpen implements BinaryElementOpen {...

    position attribute is used to choose which provider should be executed first (yours go first if you give it 0 and last if you give it some large number).

  • Next you implement open method. And here is some ugly hack I have used to quickly get class that has to be found:
    public boolean open(ClasspathInfo cpInfo, ElementHandle<? extends Element> toOpen) {
     //XXX: UGLY HACK, find some api that will get class name for field, etc (getQualifiedName fails for something else then classes)
                Field sigFields = toOpen.getClass().getDeclaredField("signatures");
                String[] signatures = (String[]) sigFields.get(toOpen);
                if (signatures != null && signatures.length > 0) {
                    String className = signatures[0];
                    //XXX: End of UGLY HACK

    Simply couldn’t find something nice to do this, and i needed class name.

  • All you have to do next is open file on line that contains stuff user clicked:
    File savedFile= new File(savedIn, name);
    FileObject dataFile = FileUtil.createData(savedFile);
    ElementOpen.open(dataFile, toOpen);
  • And return true if you succeeded or false otherwise (so Netbeans infrastructure keeps searching).

That’s all. Compile, create NBM and you’re done. Here is your custom source provider.

How to use ecj compiler in Netbeans

If you have read my post about why sometimes Eclipse ECJ compiler does not work nicely with other compilers then you maybe also need some cure for that issue.

Few days later I’ve found some, maybe not very nice, solution to get ECJ working with Netbeans IDE. Had no spare time to write about it, but i have few free minutes now and maybe someone needs that stuff also.

I’ve downloaded sources for Netbeans launcher C code (thanks God for Open Source) and modified it so it launches ECJ main class.

Then added one line in main project build.xml file:

<property name="platform.javac" value="<path_to>/ecjexec.exe"/>

(just change <path_to> to where you have saved your launcher)

This will make ant (default Netbeans build system) use ecjexec (thus ECJ) instead of javac.

Oh, you need to do one more thing: add ecj.jar to your system CLASSPATH variable.

Here is some quick’n’dirty C code you can add to Netbeans launcher to make it launch ECJ instead (sorry, no way i can attach exec here):

#include <stdlib.h>
#include <iostream>

#include "jvmlauncher.h"

int main(int argc, char** argv) {

    std::list<std::string> progArgs;
    std::list<std::string> opts;
    for (int i = 1; i < argc; i++) {
        char* arg = argv[i];

    //opts.push_back("-classpath d:/Libs/ecj/ecj.jar");

    JvmLauncher launcher;
    std::string javapath;
    bool java_exists = launcher.getJavaPath(javapath);
    std::cout << "Java: " << java_exists << " " << javapath << std::endl;
    launcher.start("org.eclipse.jdt.internal.compiler.batch.Main", progArgs, opts, true, 0);
    return (0);

You can get original code here:
http://wiki.netbeans.org/WinNB67Launcher (use platf_launcher, looks like it’s the same code I’ve downloaded few months ago)

Hope it help’s you in some way. Just remember to open/edit launcher in Netbeans 🙂

Netbeans 6.0 trunk

For last week or two I am using Netbeans 6.1 from trunk and the only thing I can say is WOW! Guys really made this thing rock! It’s fast, damn fast when working on large project and I mean really huge ones (at work I am working on Windchill and I really know what is a big java application. If you don’t know what Windchill is visit www.ptc.com and see for your self that this can make your ide sloooow). Startup time, code completion, Go To Type dialog, all this is much faster than in regular 6.0. Of course I am using some speedup options in netbeans.conf 🙂 but all of them were already used in my 6.0 installation. Currently it is something like:

“-J-client -J-Xss2m -J-Xms64m -J-Xmx300m -J-XX:PermSize=32m -J-XX:MaxPermSize=200m -J-Xverify:none -J-Dapple.laf.useScreenMenuBar=true -J-XX:+UseConcMarkSweepGC -J-XX:+CMSClassUnloadingEnabled -J-XX:+CMSPermGenSweepingEnabled -J-Duser.name=’Marek Piechut’ -J-Xoptimize -J-XX:CompileThreshold=100 -J-Djava.net.preferIPv4Stack=true –laf com.sun.java.swing.plaf.gtk.GTKLookAndFeel”

Uff, that was a long one ;-). Feel free to add some of them in your config (and don’t hestiate to comment what were your results). And most important, it’s not very buggy. It usualy makes good job and does not crash very often ;-).

%d bloggers like this: