使用来自 Eclipse 的参数调用 Java main 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/373328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoking Java main method with parameters from Eclipse
提问by Thilo
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).
在开发(和调试)期间, 直接从 Eclipse 内部运行 Java 类的public static void main(String[] argv)方法(使用 Run As 上下文菜单)非常有用。
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).
是否有类似的快速方法来为运行指定命令行参数?我现在要做的是转到“运行对话框”,单击各种设置到选项卡,在那里我可以指定 VM 和程序参数并在那里输入它们。步骤太多,而且我不想将更永久的运行时配置设置与一次性调用参数混合在一起。我想要的是在某处选中一个框(或者有一个单独的菜单项“使用命令行作为 Java 应用程序运行”),然后每次都提示输入命令行(有一个很好的历史记录)。
采纳答案by MetroidFan2002
This answer is based on Eclipse 3.4, but should work in older versions of Eclipse.
这个答案基于 Eclipse 3.4,但应该适用于旧版本的 Eclipse。
When selecting Run As..., go into the run configurations.
选择 Run As... 时,进入运行配置。
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).
在 Java 运行配置的 Arguments 选项卡上,将变量 ${string_prompt} 配置为出现(您可以单击变量获取它,或复制它以直接设置它)。
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.
每次您使用该运行配置(命名好以便以后使用)时,系统都会提示您输入命令行参数。
回答by Uri
AFAIK there isn't a built-in mechanism in Eclipse for this.
AFAIK Eclipse 中没有为此内置机制。
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.
您可以获得的最接近的是创建一个包装器,提示您输入这些值并调用(硬编码)main。只要您不清除终止的进程,您就可以获得执行历史记录。对此的两种变体是使用 JUNit,或者使用注入或参数,以便您的包装器始终连接到其主要的正确类。
回答by Yuval Adam
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.
IMO,您建议的功能似乎有点矫枉过正。
回答by Esko
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:
uri是错误的,Eclipse有直接给main方法加参数的方法,但是参数不会很灵活(允许一些动态参数)。您需要执行以下操作:
- Run your class once as is.
- Go to
Run -> Run configurations...
- From the lefthand list, select your class from the list under
Java Application
or by typing its name to filter box. - Select Arguments tab and write your arguments to
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. - Run your class again just like in step 1.
- 按原样运行一次您的课程。
- 去
Run -> Run configurations...
- 从左侧列表中,从列表中选择您的班级,
Java Application
或通过键入其名称来过滤框。 - 选择参数选项卡并将您的参数写入
Program arguments
框中。以防万一不清楚,它们以空格分隔,因此"a b c"
(不带引号)意味着您将参数 a、b 和 c 传递给您的程序。 - 再次运行您的类,就像在步骤 1 中一样。
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.
然而,我确实建议使用 JUnit/wrapper 类,就像 Uri 所说的那样,因为这样你可以比这样做更好地控制实际参数。
回答by Romain Linsolas
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...
将所有参数放在一个属性文件中(一个参数 = 此文件中的一个属性),然后在您的主方法中加载此文件(使用Properties.load(*fileInputStream*)
)。因此,如果您想修改一个参数,您只需要编辑您的 args.properties 文件,然后启动您的应用程序,而无需执行更多步骤...
Of course, this is only for development purposes, but can be really helpfull if you have to change your arguments often...
当然,这仅用于开发目的,但如果您必须经常更改参数,这真的很有帮助......
回答by Neyydupakkam Sundarasekaran
If have spaces within your string argument, do the following:
如果字符串参数中有空格,请执行以下操作:
Run > Run Configurations > Java Application > Arguments > Program arguments
运行 > 运行配置 > Java 应用程序 > 参数 > 程序参数
- Enclose your string argument with quotes
- Separate each argument by space or new line
- 用引号将字符串参数括起来
- 用空格或换行分隔每个参数