简体   繁体   中英

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"

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. I am not exactly sure if it will work for stdin, but it is worth a try.

The way you do this in VS is to set the command line arguments when debugging to "< stdin.txt". Maybe the same will work in 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). Eclipse doesn't directly support doing what you want, but achieving this is relatively simple to do.

In your project, right click and select New->File and name it streamit.bat. Select the new file, right click and Open With->Text editor. Enter the following line and save the file: 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. In the Working directory field, hit Browse workspace and select your project. In the Arguments field enter: StreamClass input.txt

(this assumes input.txt is located in your project) Now hit Run. It'll launch streamit.bat, replacing %1 and %2 with the arguments specified, and the results will be displayed in the Console view. You can repeat this run by clicking the Run button with the red toolbox on the toolbar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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