简体   繁体   English

使用Eclipse中的参数调用Java main方法

[英]Invoking Java main method with parameters from Eclipse

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. 我现在做的是转到“运行对话框”,单击各种设置到选项卡,我可以在其中指定VM和程序参数并在那里输入。 Too many steps, plus I do not want to mix the more permanent runtime configuration settings with the one-off invokation parameters. 步骤太多,而且我不希望将更永久的运行时配置设置与一次性invokation参数混合使用。 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). 我想要的是在某个地方检查一个框(或者有一个单独的菜单项“使用命令行运行为Java应用程序”),然后每次都提示输入命令行(具有良好的历史记录)。

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. 每次使用该运行配置(将其命名为以便以后使用)时,系统都会提示您输入命令行参数。

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). Uri是错误的,有一种方法可以直接在Eclipse中向main方法添加参数,但是参数不是很灵活(允许一些动态参数)。 Here's what you need to do: 这是你需要做的:

  1. Run your class once as is. 按原样运行你的课程。
  2. Go to Run -> Run configurations... 转到Run -> Run configurations...
  3. From the lefthand list, select your class from the list under Java Application or by typing its name to filter box. 从左侧列表中,从Java Application下的列表中选择您的类,或者通过键入其名称来过滤框。
  4. Select Arguments tab and write your arguments to Program arguments box. 选择Arguments选项卡并将参数写入Program arguments框。 Just in case it isn't clear, they're whitespace-separated so "abc" (without quotes) would mean you'd pass arguments a, b and c to your program. 如果不清楚,它们是空格分隔的,所以"abc" (没有引号)意味着你将参数a,b和c传递给你的程序。
  5. Run your class again just like in step 1. 再次运行您的课程,就像在第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所说的那样,因为这样你可以更好地控制实际参数而不是这样做。

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. 最接近的是创建一个包装器,提示您输入这些值并调用(硬编码)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. 对此的两个变体是要么使用JUNit,要么使用注入或参数,以便您的包装器始终连接到其主要的正确类。

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。

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*) ). 将所有参数放在属性文件中(一个参数=此文件中的一个属性),然后在main方法中,加载此文件(使用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... 因此,如果要修改一个参数,只需编辑args.properties文件,启动应用程序,无需执行更多步骤...

Of course, this is only for development purposes, but can be really helpfull if you have to change your arguments often... 当然,这仅用于开发目的,但如果您经常需要更改参数,则可能非常有用...

If have spaces within your string argument, do the following: 如果字符串参数中包含空格,请执行以下操作:

Run > Run Configurations > Java Application > Arguments > Program arguments 运行>运行配置> Java应用程序>参数>程序参数

  1. Enclose your string argument with quotes 用引号括起你的字符串参数
  2. Separate each argument by space or new line 用空格或换行分隔每个参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM