Victor Resnov
Victor Resnov

Reputation: 292

Java on repl.it java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it

Before I get into the problem, since I am not using a Linux machine or anything like that and I am using repl.it, the generic solution of export DISPLAY=:0.0 or setenv DISPLAY :0.0 will not work. This is simply a java file on repl.it.

So basically, I have a program that writes custom user-defined information to a text file, then reads it. The reading algorithm and everything works. Here's my code for the window:

Here is my Window class that has the code for the display:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.io.File;
import java.util.Scanner;

public class Window extends JFrame {

  //Typing Area:
  private JTextField txtEnter = new JTextField();

  //Chat Area:
  private static JTextArea txtChat = new JTextArea();

  public Window(String name) {
    //Frame Attributes:
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600, 600);
    this.setVisible(true);
    this.setResizable(false);
    this.setLayout(null);
    this.setTitle("name");

    //txtChat Attributes:
    txtChat.setLocation(15, 5);
    txtChat.setSize(560, 510);

    this.add(txtChat);

    try {
      File f = new File(name = ".txt");
      Scanner scan = new Scanner(f);
      txtChat.append("File name: " + f.getName() + "\n");
      txtChat.append("File Size in Bytes: " + f.length() + " bytes\n");
      txtChat.append("\nFile Contents:\n\n");

      while(scan.hasNext()) {
        txtChat.append(scan.nextLine() + "\n");
      }
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

}

This is how I use the method:

new Window(file.getName());

Is there a variable or something that I can set to get rid of this issue?

Upvotes: 0

Views: 977

Answers (2)

axelclk
axelclk

Reputation: 958

Use System.in for your input and System.out for the output like in this example:

You can't use the swing GUI classes: JTextField or JTextArea in headless java repl.it.

As Matt mentioned you can use the special https://repl.it/languages/java_swing

This will at least compile but misses the file of course:

import java.io.File;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Main extends JFrame {

    // Typing Area:
    private JTextField txtEnter = new JTextField();

    // Chat Area:
    private JTextArea txtChat = new JTextArea();

    public Main(String name) {
        // Frame Attributes:
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 600);
        this.setVisible(true);
        this.setResizable(false);
        this.setLayout(null);
        this.setTitle("name");

        // txtChat Attributes:
        txtChat.setLocation(15, 5);
        txtChat.setSize(560, 510);

        this.add(txtChat);

        try {
            File f = new File(name = ".txt");
            Scanner scan = new Scanner(f);
            txtChat.append("File name: " + f.getName() + "\n");
            txtChat.append("File Size in Bytes: " + f.length() + " bytes\n");
            txtChat.append("\nFile Contents:\n\n");

            while (scan.hasNext()) {
                txtChat.append(scan.nextLine() + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // creating instance of JFrame
        Main f = new Main("test");

        f.setSize(400, 500);
        f.setLayout(null);
        // make the frame visible
        f.setVisible(true);
    }
}

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109547

Small errors:

  • You forgot: scan.close(); at the end.
  • txtChat should not be static but a field of Window.

Somewhere there probably is:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> new Window("x.txt").setVisible(true));
}

The error? Maybe the repl is headless (=only console)?

Maybe you run in a Docker container or such. Which would be my first guess.

Upvotes: 0

Related Questions