1

I usually run this program via a command line like so:

java Program <TestClass.java

Which as I understand, forces the contents of TestClass.java to the console as user input. i.e. It would be like executing

java Program

and then typing what ever is in TestClass.java

My problem is getting this happening in Eclipse. I can't figure out how to do it.
I would have thought that adding

<TestClass.java

to the program arguments in the run configuration would work, but it seems not.
Any suggestions?

4
  • Is it possible that it's actually working but you have the wrong working path? Commented May 3, 2011 at 8:37
  • TestClass.java is in the same folder as Program, so I wouldn't think it's that? Commented May 3, 2011 at 8:39
  • Eclipse supports only JVM/Program arguments. But you are passing DOS commnad < Testclasss.java as argumaent so it wont't work in eclipse. Commented May 3, 2011 at 8:53
  • OK, that makes more sense, is there a way to do what I'm talking about in eclipse, or do I have to write up some code to read in the file? Commented May 3, 2011 at 8:55

1 Answer 1

1

How about adding this on top of your main.

InputStream in;
if (args.length > 0) {
  in = new FileInputStream(args[0]);
} else {
  // fallback
  in = System.in;
}

And then you add the filename as an argument, as if you're running java Program TestClass.java. This way, it will work whether you run it as before or using the filename as an argument.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.