2

How to display the NetBeans ide java program console output onto the windows command line output? please help as I am new to this...thanks in advance here I must execute the program from NetBeans but only the output must be displayed on my windows command line.

7
  • no not running on command prompt but diplaying the output of NetBeans program run in NetBeans onto command prompt Commented Aug 9, 2015 at 9:47
  • when you run it in cmd you can see the output in cmd . Commented Aug 9, 2015 at 9:49
  • but I don't want to run in cmd but I want to run in Netbeans and see the output only on cmd. Commented Aug 9, 2015 at 9:56
  • 1
    Can you explain why you need to run it from one place, but send output to another? Why can't you run and see output both in the same place? You can do both in Netbeans and you can do both in cmd. Commented Aug 9, 2015 at 10:28
  • 1
    @user4800039 Explain why want this? Commented Aug 9, 2015 at 12:52

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.