4

How can I have Netbeans run my Java project inside a system terminal rather than its built-in terminal?

I looked around and apparently I am supposed to do Project Properties->Run->Console Type but this has regrettably been removed from the project configuration panel at some point in history.

Every other thread I can dig up on the internet regarding this issue has - predictably - no replies.

I know I can run the jar file from the command-line but an integrated solution would be helpful.

I am using Netbeans 7.

Thomas
  • 3,321
  • 1
  • 21
  • 44

1 Answers1

0

I am not sure this can be done for an Ant project but it can be done for a Maven project.

  • Create a Maven Project. File -> New Project. Select Category "Maven" and Project Type "Java Application". Click Next and then Finish to accept project defaults.
  • Add a Main class with a public static void main(String args[]) method. Expand Source Packages in the Projects window. Select any package. Right-click -> New -> "Java Class".

Add something to wait for output before exiting or your terminal will exit without your having time to view the output.

public static void main(String[] args) {
    System.out.println("hello");
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
        br.readLine();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  • Select the project in the projects window. Right-click for pop-up. Choose Properties. Select category "Run". Click the Browse button next to Main class and select the Main class.
  • Run the project one time normally with the green triangle on the toolbar, menu Run-> Run Project or F6.
  • Expland the "Project Files" node in the projects window. Double-click "nbactions.xml".
  • Change the Properties for the "run" action. Change executable to your terminal and add the appropriate flags and java to the arguments.

eg. From :

        <properties>
            <exec.args>-classpath %classpath wshackle.mavenproject2.Main</exec.args>
            <exec.executable>java</exec.executable>
        </properties>

to :

        <properties>
            <exec.args>-x java -classpath %classpath wshackle.mavenproject2.Main</exec.args>
            <exec.executable>gnome-terminal</exec.executable>
        </properties>

or for Windows:

        <properties>
            <exec.args>/c java -classpath %classpath wshackle.mavenproject2.Main</exec.args>
            <exec.executable>cmd</exec.executable>
        </properties>
  • Save and close this file.
  • Run the project. It should now open up in an external terminal.

Note: I provided the same answer here: display netbeans java output terminal on the command line

Community
  • 1
  • 1
WillShackleford
  • 6,918
  • 2
  • 17
  • 33