简体   繁体   English

在eclipse中更改System.in以从文件读取

[英]Changing System.in in eclipse to read from a file

This seems like kind of a silly question but is there a way to change the standard in in eclipse. 这似乎是一个愚蠢的问题,但是有办法改变日食的标准。 If I was doing this from the windows console it would look like "java myprogram < stdin.txt" 如果我是从Windows控制台执行此操作,则它将看起来像“ java myprogram <stdin.txt”

I am using Eclipse 3.4 and I see a place to enter a file for stdin/stdout on the Common tab of a Java Application Run configuration. 我正在使用Eclipse 3.4,并且在Java Application Run配置的“常用”选项卡上看到了一个为stdin / stdout输入文件的位置。 I am not exactly sure if it will work for stdin, but it is worth a try. 我不确定它是否适用于stdin,但值得一试。

The way you do this in VS is to set the command line arguments when debugging to "< stdin.txt". 在VS中执行此操作的方法是在调试为“ <stdin.txt”时设置命令行参数。 Maybe the same will work in Eclipse? 也许同样适用于Eclipse? I'd check but I don't have it on this machine. 我会检查,但我在这台机器上没有它。

There is a (fairly ugly) solution, as pointed out in this comment 评论中所指出的,有一个(相当丑陋的)解决方案

Since the mailing list archive I originally linked to seems to have disappeared, here is a copy of what was there (in turn copied from here ): 由于我最初链接到的邮件列表档案似乎已经消失,因此这里是其中的一份副本(又从此处复制):

The ability to redirect standard input to come from a file is OS dependent (actually it's a function of the OS's shell, in this case your DOS window). 将标准输入重定向到来自文件的能力取决于操作系统(实际上,它是操作系统外壳的功能,在本例中为DOS窗口)。 Eclipse doesn't directly support doing what you want, but achieving this is relatively simple to do. Eclipse不直接支持您想要做的事情,但是实现这一点相对容易。

In your project, right click and select New->File and name it streamit.bat. 在项目中,右键单击并选择“新建”->“文件”,并将其命名为streamit.bat。 Select the new file, right click and Open With->Text editor. 选择新文件,右键单击并打开方式->文本编辑器。 Enter the following line and save the file: java %1 < %2 输入以下行并保存文件:Java%1 <%2

Now create a External tool launch configuration - from the Run menu go to Run->External tools->External tools... Select Program and hit New, then in the Location field, hit Browse workspace and select streamit.bat from your project. 现在创建一个外部工具启动配置-从“运行”菜单转到“运行”->“外部工具”->“外部工具...”,选择“程序”并单击“新建”,然后在“位置”字段中,单击“浏览”工作区,然后从项目中选择streamit.bat。 In the Working directory field, hit Browse workspace and select your project. 在工作目录字段中,单击浏览工作空间,然后选择您的项目。 In the Arguments field enter: StreamClass input.txt 在“参数”字段中输入:StreamClass input.txt

(this assumes input.txt is located in your project) Now hit Run. (这假设input.txt位于您的项目中)现在点击运行。 It'll launch streamit.bat, replacing %1 and %2 with the arguments specified, and the results will be displayed in the Console view. 它将启动streamit.bat,用指定的参数替换%1和%2,结果将显示在控制台视图中。 You can repeat this run by clicking the Run button with the red toolbox on the toolbar. 您可以通过单击工具栏上带有红色工具箱的“运行”按钮来重复此运行。

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

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