使用 Eclipse 中的参数调用 Java main 方法
在开发期间(以及调试),直接从 Eclipse 内部运行 Java 类的 public static void main(String[] argv) 方法(使用 Run As 上下文菜单)非常有用。
是否有类似的快速方法来指定运行的命令行参数? 我现在要做的是转到“运行对话框”,单击各种设置到选项卡,我可以在其中指定虚拟机和程序参数并在其中输入它们。 步骤太多,而且我不想将更永久的运行时配置设置与一次性调用参数混合在一起。 我想要的是在某个地方选中一个框(或者有一个单独的菜单项“使用命令行作为 Java 应用程序运行”),然后每次都会提示输入命令行(有很好的历史记录)。
During development (and for debugging) it is very useful to run a Java class' public static void main(String[] argv) method directly from inside Eclipse (using the Run As context menu).
Is there a similarily quick way to specify command line parameters for the run?
What I do now is go to the "Run Dialog", click through the various settings to the tab where I can specify VM and program arguments and enter them there.
Too many steps, plus I do not want to mix the more permanent runtime configuration settings with the one-off invokation parameters.
What I want instead is to check a box somewhere (or have a separate menu item "Run as Java application with command line") and then be prompted for the commandline every time (with a nice history).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
另一个想法:
将所有参数放在一个属性文件中(一个参数 = 该文件中的一个属性),然后在 main 方法中加载该文件(使用
Properties.load(*fileInputStream*)
)。因此,如果您想修改一个参数,您只需要编辑您的 args.properties 文件,然后启动您的应用程序,无需执行更多步骤...
当然,这仅用于开发目的,但如果您必须经常改变你的论点......
Another idea:
Place all your parameters in a properties file (one parameter = one property in this file), then in your main method, load this file (using
Properties.load(*fileInputStream*)
).So if you want to modify one argument, you will just need to edit your args.properties file, and launch your application without more steps to do...
Of course, this is only for development purposes, but can be really helpfull if you have to change your arguments often...
我不确定您的用途是什么,但我发现通常我使用不超过几个命令行参数很方便,因此每个场景都会获得一个运行配置,我只需从运行历史记录中选择我想要的一个。
在我看来,你建议的功能似乎有点过分了。
I'm not sure what your uses are, but I find it convenient that usually I use no more than several command line parameters, so each of those scenarios gets one run configuration, and I just pick the one I want from the Run History.
The feature you are suggesting seems a bit of an overkill, IMO.
AFAIK Eclipse 中没有为此提供内置机制。
最接近的方法是创建一个包装器,提示您输入这些值并调用(硬编码的)main。 只要您不清除终止的进程,您就可以获得执行历史记录。 对此的两种变体是使用 JUNit,或者使用注入或参数,以便您的包装器始终连接到其 main 的正确类。
AFAIK there isn't a built-in mechanism in Eclipse for this.
The closest you can get is to create a wrapper that prompts you for these values and invokes the (hardcoded) main. You then get you execution history as long as you don't clear terminated processes. Two variations on this are either to use JUNit, or to use injection or parameter so that your wrapper always connects to the correct class for its main.
如果字符串参数中有空格,请执行以下操作:
Run > 运行配置> Java应用程序> 参数> 程序参数
If have spaces within your string argument, do the following:
Run > Run Configurations > Java Application > Arguments > Program arguments
这个答案基于 Eclipse 3.4,但应该适用于旧版本的 Eclipse。
选择运行方式...时,进入运行配置。
在 Java 运行配置的“参数”选项卡上,配置要显示的变量 ${string_prompt}(您可以单击变量来获取它,或复制它以直接设置它)。
每次使用该运行配置(命名好以便稍后使用)时,系统都会提示您输入命令行参数。
This answer is based on Eclipse 3.4, but should work in older versions of Eclipse.
When selecting Run As..., go into the run configurations.
On the Arguments tab of your Java run configuration, configure the variable ${string_prompt} to appear (you can click variables to get it, or copy that to set it directly).
Every time you use that run configuration (name it well so you have it for later), you will be prompted for the command line arguments.
uri错误,Eclipse中有一种方法可以直接在main方法中添加参数,但是参数不会很灵活(允许一些动态参数)。 您需要执行以下操作:
运行 -> 运行配置...
Java 应用程序
下的列表中选择您的类,或者在筛选框中键入其名称。"ab c"
(不带引号)意味着您将参数 a、b 和 c 传递给您的程序。不过,我确实建议使用 JUnit/包装器类,就像 Uri 所说的那样,因为这样您可以比这样做更好地控制实际参数。
Uri is wrong, there is a way to add parameters to main method in Eclipse directly, however the parameters won't be very flexible (some dynamic parameters are allowed). Here's what you need to do:
Run -> Run configurations...
Java Application
or by typing its name to filter box.Program arguments
box. Just in case it isn't clear, they're whitespace-separated so"a b c"
(without quotes) would mean you'd pass arguments a, b and c to your program.I do however recommend using JUnit/wrapper class just like Uri did say since that way you get a lot better control over the actual parameters than by doing this.