38

Is there any way to configure the command line args to intellij for stdin redirection?

Something along the lines of:

Run | Edit Run Configurations | Script Parameters

/shared/java/paf-rules.properties 2 < /shared/java/testdata.csv
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • This should be merged with [IntelliJ IDEA: Run java with args from external file](https://stackoverflow.com/questions/42867756/intellij-idea-run-java-with-args-from-external-file) – jakub.g Oct 16 '18 at 09:22
  • 1
    This is specific for scala (and precedes the other one by four years) so merging not required. – WestCoastProjects May 04 '19 at 00:50

2 Answers2

15

Unfortunately, no - at least not directly in run configurations.

The best you can do, afaik, is either to:

  • modify your script / program to run either with no args (reads System.in) or with a filename argument (reads the file)

  • make a wrapper script / program which acts in the manner above.

Hope this helps,

vikingsteve

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
1

Here is a template for a solution that has options for:

  • stdin
  • file
  • "heredoc" within the program (most likely useful for testing)

.

  val input = """ Some string for testing ... """

  def main(args: Array[String]) {
    val is = if (args.length >= 1) {
      if (args(0) == "testdata") {
        new StringInputStream(input)
      } else {
        new FileInputStream(args(0))
      }
    } else {
      System.in
    }
    import scala.io._
    Source.fromInputStream(is).getLines.foreach { line =>

This snippet requires use of a StringInputStream - and here it is:

  class StringInputStream(str : String) extends InputStream {
    var ptr = 0
    var len = str.length
    override def read(): Int = {
        if (ptr < len) {
          ptr+=1
          str.charAt(ptr-1)
        } else {
         -1
        }
    }
  }
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560