How to run single Java file from a project in Eclipse?

前端 未结 7 839
慢半拍i
慢半拍i 2020-12-29 22:54

I want to run one Java file from a project in Eclipse. There is code to insert into database in that Java file, I want to check from that single file whether its working or

7条回答
  •  一向
    一向 (楼主)
    2020-12-29 23:09

    It's important to note that you can't just run arbitrary Java code, but have to have some structure set up (for example, if you're going to call a method, what arguments are you passing in?) To run a specific piece of Java code, you should consider creating a main method in that class that just launches some specific piece of code under the constraints that you'd like. One way to do this would be to add a method public static void main(String[] args) containing the code you want to run. Once you've done this, you can right-click on it, choose the "Run as..." option, and select "Java application" to run the program you've written.

    Alternatively, if you just want to check whether some specific piece of code is working, you could consider using JUnit to write unit tests for it. You could then execute the JUnit test suite from Eclipse to check whether that specific piece of code is functioning correctly. This is what I would suggest doing, as it makes it possible to test the software through every step of the development.

    Hope this helps!

提交回复
热议问题