I have created a web application which makes use of JOptionPane to display some dialogs to the users such as 'Record not found etc'. I did this development in Windows and everything was working fine.
I was then asked to deploy the project war on a remote server which is a RHEL machine without any GUI (similar to AWS machines). When i run my we application over there i get the below mentioned exception wherever there is a JOptionPane statement.
Exception in thread "http-bio-8080-exec-6" java.lang.InternalError: Can't connect to X11 window server using 'localhost:1.0' as the value of the DISPLAY variable. at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method) at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:62) at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:178) at java.security.AccessController.doPrivileged(Native Method) at sun.awt.X11GraphicsEnvironment.(X11GraphicsEnvironment.java:142) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:188) at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82) at sun.swing.SwingUtilities2.isLocalDisplay(SwingUtilities2.java:1393) at javax.swing.plaf.metal.MetalLookAndFeel.initComponentDefaults(MetalLookAndFeel.java:1563) at javax.swing.plaf.basic.BasicLookAndFeel.getDefaults(BasicLookAndFeel.java:147) at javax.swing.plaf.metal.MetalLookAndFeel.getDefaults(MetalLookAndFeel.java:1599) at javax.swing.UIManager.setLookAndFeel(UIManager.java:530) at javax.swing.UIManager.setLookAndFeel(UIManager.java:570) at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1320) at javax.swing.UIManager.initialize(UIManager.java:1407) at javax.swing.UIManager.maybeInitialize(UIManager.java:1395) at javax.swing.UIManager.getDefaults(UIManager.java:644) at javax.swing.UIManager.getString(UIManager.java:790) at javax.swing.UIManager.getString(UIManager.java:807) at javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:610)
I have already tried to use export DISPLAY also tried
System.setProperty("java.awt.headless", "true");
boolean headless = GraphicsEnvironment.isHeadless();
but still my problem is not resolved? Any inputs? Should i not be using JOptionPane in a web application? if not then what is a better option? alternate of JOption?
You absolutely cannot use a JOptionPane in a web application. And don't even think of using any other of the JWhatever classes in Swing.
JOptionPane, and various other Swing components, appear only on the machine they are running on. So, even if you could get past the exception you are encountering, and display the JOptionPane somehow, the JOptionPane would appear on the server1. It would not appear in the user's browser, so the user wouldn't see it.
So, suppose this JOptionPane has appeared on the server. How is a user supposed to read it, or dismiss it? All they have access to is their browser.
The simple answer is that they can't.
Worse still, JOptionPanes are modal, in that the call to create them doesn't return until the dialog is dismissed. If a user entered a model number that did not exist, your web application would appear to hang, as it could not finish serving the page until the JOptionPane was dismissed. With a few more users using your system, the entire application would eventually grind to a halt as more and more of the web application's worker threads get stuck waiting for JOptionPanes to be dismissed.
Instead, you need to send the error message back to the browser somehow.
If the user enters a model number that does not exist, I would recommend redirecting back to the page where the model number is entered, and showing on this page a message that makes it clear to the user that they were redirected back because they entered a nonexistent model number.
Your code may have been working for you on your machine, but that would only have been because you were browsing your web application from the same machine that the web server was running on.
1: Technically you could show this JOptionPane to a client if you had an X server on the client machine and could set it up as Jim Garrison describes, but that is most certainly not something you can assume.