Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

828
Vistas
IntelliJ IDEA: Run java with args from external file

I want to run a java class with args supplied as a file.

On shell, i can do

    java SomeClass < 1.txt

Is there any equivalent for this on intellij and/or gradle.

I tried on IntelliJ IDEA -> edit configurations. But, the argument is not getting passed.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

UPDATE:

This feature is now available for some Run/Debug configurations. At the moment supported types for Java-based run configurations are: Application, Java Scratch, JUnit, JarApplication.

Use the Redirect input from option:

Redirect input from


Original answer from 2017 with the workaround:

IntelliJ IDEA doesn't support it at the moment:

  • IDEA-88717 No way to configure STDIN reading from a file at Run/Debug configurations

You can adjust the code so that it accepts the file name as a parameter and opens it for reading. Or you can create a wrapper class that will redefine System.in, then start your original Main class:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MainWrapper {
  public static void main(String[] args) throws IOException {
    FileInputStream is = new FileInputStream(new File("1.txt"));
    System.setIn(is);
    Main.main(args);
  }
}

Make sure to either specify the full path to the file or to change the working directory in IntelliJ IDEA Run/Debug configuration to the location of 1.txt.

Now you can run MainWrapper class instead of the Main class and it will work the same as running

java Main < 1.txt

If you need to test with different file names, replace new File("1.txt") with args[0] and pass the file name in the MainWrapper Run/Debug configuration Program arguments field.

about 4 years ago · Santiago Trujillo Denunciar

0

You can actually do that now, starting from version 2018.3:
- Select "Run/Debug configurations"
- Set path to input file at "Redirect input from" field

enter image description here

about 4 years ago · Santiago Trujillo Denunciar

0

The < 1.txt syntax mean that the command processor ensures that standard input (System.in in Java) provides the contents of the 1.txt file. The typical Java launcher do not support this.

You can emulate this by manually rewire System.in to read from the file using your own Java snippet to do this.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda