How to Run a Simple Java Program in Eclipse?

后端 未结 5 2250
醉话见心
醉话见心 2020-12-16 18:34

As you can probably understand from the question itself, I\'m new to Java. I was given an exercise to write a Java program which receives a character, prints it and the next

相关标签:
5条回答
  • 2020-12-16 19:18

    Right click on your java file in project explorer of your eclipse. Then Run As> Run Configuration

    enter image description here Then you will get a window. Like-

    enter image description here

    Click on Arguments Tabs, and then write some text there, may be a character.

    And then Click on Apply button and Run Button.

    0 讨论(0)
  • 2020-12-16 19:19

    This is a great question with some very good answers. I would like to add some pointers about how to debug your own program. Debugging is as important (if not more important) than writing code.

    For one thing, Eclipse has some great debugging features. You can use this debugger to find problems in your code. I suggest that you learn how to use it. In particular, you can set watches for variables to see what value they have as you step through the execution of your code.

    Alternatively, you can add calls to System.out.println() to print out the values of any variables. For example, adding the following line at the beginning of your code might help you narrow down the problem:

    System.out.println(args[0]);
    

    This would also give an ArrayIndexOutOfBoundsException if no command-line arguments are given. Then you could do something like

    System.out.println(args.length);
    

    which would print out 0. This then gives you a clue as to where the problem is.

    Of course, even when you get to this point, you still might not know how to solve the problem. This is where sites like StackOverflow come in handy.

    Good luck with your Java experience. Please come back when you need more help.

    0 讨论(0)
  • 2020-12-16 19:20

    The default run configuration in Eclipse runs a Java program without any arguments, hence the ArrayIndexOutOfBoundsException. Your code is trying to get first element of the args array when there aren't any!

    You can edit the run configuration to provide the arguments to run your program with. Then it should not throw this exception.

    However, a good practice is to check the size of array before accessing it's elements, more so when the array is coming as an argument from outside of your code.

    0 讨论(0)
  • 2020-12-16 19:24
    • Select "Run -> Run Configurations" from the menu.
    • Search for you project in the list on the left and select it.
    • Select the "Arguments" tab on the right.
    • Write the argument you want to pass to the programm in "Programm arguments".
    • Click "Run"
    0 讨论(0)
  • 2020-12-16 19:30

    If your Run Configurations are in place (as already shown in above answers):

    Shortcut to Run a class is:

    Ctrl + F11

    0 讨论(0)
提交回复
热议问题