Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JOptionPane No X11 DISPLAY error

 
Tarun Kumar Agrawal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to popup one message using JOptionPane. I am getting following error:



How can i fix this?
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By running the program from a windowed environment, like GNOME, KDE, Enlightenment, etc. Judging from that exception, you are running the program on a command line without any windowing system running, probably a server. So where are you going to display the JOptionPane?

If you get a HeadlessException you can sometimes prevent it by using the -Djava.awt.headless=true JVM property (which comes before the class name), but only if you don't really need the windowed environment. Which you do in this case.
 
Tarun Kumar Agrawal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have deployed my code on one machine. I am running this from another machine using url: http://machine-ip:8080. Everything is displaying properly except that JOptionPane Message.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course that won't work. The servlet / JSP code runs on the server, not on the client. That means that the JOptionPane dialog will be shown on the server, and there's no graphical environment there (or else you wouldn't get this exception). You'll have to use JavaScript to show messages to the user.
 
Tarun Kumar Agrawal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i call Javascript function from java servlet?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot. The servlet runs on the server, the JavaScript code in a HTML page runs on the client. The servlet sends back data (often in the form of a HTML page), which can also contain JavaScript. You could make the servlet return a page with some script in it that is run when the browser loads the page.

A web application does not work in exactly the same way as a desktop application, where you can display a message box at any time. You have to be aware of the HTTP request-response cycle, and what code runs where (server or client) and when.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to show a JavaScript dialog from a servlet is indeed by simply letting the generated HTML page contain code to show the dialog:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic