Provide Runtime Arguments for Selected Class Files in NetBeans IDE 4.1

In the Project Properties dialog box, the Arguments field specifies the runtime argument(s) for the main class. But what if you're not running a main class? How do you specify a runtime argument? The Project Properties dialog box doesn't support this scenario. But... Ant does! You can use Ant to create the application.args property which is used by the IDE when a file is run. However, you must make sure that the property is created at the right time -- before the project's properties are initialized. After the properties are initialized, you can't create the property again (you could override it in Ant, I guess, although I don't know how to do that yet). So, here's the target:

<target name="-pre-init" description="Set Runtime Arguments">
  <input message="Runtime arguments:"

Next, you need to go to the Project Properties dialog box and delete whatever class is specified as the main class. This means that when you run the project, you're prompted to select a new main class. That's a bit inconvenient, but not too bad. So, when you run the project, you select the new 'main class' and then the -pre-init target above is called -- this asks you what runtime argument you'd like to use. (You can specify any acceptable arguments in the Ant target above, of course. If you want a blank slate rather than a pre-defined set of options, just don't define the validargs property in the target.) Then the run target is called and the application is run using the argument that you selected.

Note, however, that the above scenario only works when you run the application, not when you run a file. In other words, when you right-click a file and choose Run File, the application.args is set, but somehow (not sure why, but probably because it sets application-level arguments, not file-level arguments) not used in the target that runs the file. So, you have to right-click the project and then choose Run Project for the desired effect to occur.



Posted by ggg on November 07, 2005 at 05:31 PM PST #

Shouldn't this just be supported by netbeans itself? It's now a bit of a pain to use and I need it often. Kees

Posted by guest on February 28, 2006 at 06:04 PM PST #

Hi Kees, there's a plug-in module in my update center that covers this functionality. Yes, it should be supported out of the box, unfortunately it currently isn't.

Posted by Geertjan on March 02, 2006 at 04:54 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2016