如何从Eclipse中的项目运行单个Java文件? [英] How to run single Java file from a project in Eclipse?

查看:719
本文介绍了如何从Eclipse中的项目运行单个Java文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Eclipse中的一个项目运行一个Java文件。有可以插入到该Java文件的数据库中的代码,我想从该单个文件检查它是否工作。有没有办法从项目中运行单个Java文件?

解决方案

重要的是要注意,你不能只是运行任意的Java代码,而是必须设置一些结构例如,如果要调用一个方法,传入哪些参数?)要运行特定的Java代码,您应该考虑创建一个 main 方法那个类只是在你想要的约束下启动一些特定的代码片段。一种方法是添加一个包含要运行的代码的方法 public static void main(String [] args)完成此操作后,您可以右键单击它,选择运行为...选项,然后选择Java应用程序运行您编写的程序。



或者,如果你只想检查一些特定的代码是否正常工作,你可以考虑使用 JUnit 为其编写单元测试。然后,您可以执行 Eclipse中的JUnit测试套件 来检查特定的代码段是否正常运行。这是我建议做的,因为它可以通过开发的每一步测试软件。



希望这有帮助!


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 not. Is there any way to run a single Java file from a project?

解决方案

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!

这篇关于如何从Eclipse中的项目运行单个Java文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆