Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netbeans java, open a cmd-window on build

I wish to be able to do the following under Windows (Windows 7):

  1. Press Build on a Netbeans Project
  2. Build the project
  3. Have it open and execute the generated jar in a cmd-window

Why do I want this? To be sure that it works, and to be able to send a Control-C to the terminal/console.

I think it could be achieved via Ant, but please help me on how to do this.

like image 684
skiwi Avatar asked Feb 13 '26 20:02

skiwi


1 Answers

You can do this by extending NetBeans' build script. To do that, do the following:

  1. Open the "Files" window (Window -> Files)
  2. Open the nbbuild.xml file in the directory with the name of your project.
  3. Add the following code to it:

    <target name="-post-jar">
      <exec executable="cmd.exe" dir="${dist.dir}">
        <arg value="/k"/>
        <arg value='start "MyApp" java -jar ${dist.jar}'/>
      </exec>
    </target> 
    

Now when you build your project the -post-jar target will be called once NetBeans is finished with the build. This will start a commandline window to run your project. As an alternative you could also override the run target. An example on how to do that is inside the nbbuild.xml file.

Note that you have to use the start command, otherwise the console window won't be visible (at least I don't know of any other way).

This also assumes that you have a self-executable jar file that can be started using java -jar .... If that is not the case you will need to assemble the full classpath to be passed to the java command.

Some more information about customizing NetBeans' build process can be found in the online help: http://docs.oracle.com/cd/E40938_01/doc.74/e40142/create_japps.htm#CHDDAHEB

Having said all that: to verify that your application works correctly, it would be a much better choice to write proper unit tests.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!