2
JFileChooser chooser = new JFileChooser();
JDialog dialog=new JDialog();
dialog.setAlwaysOnTop(true);
/*System.out.println("is always on top?"+dialog.isAlwaysOnTop());*/
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(dialog);

String path = null;

if(returnVal == JFileChooser.APPROVE_OPTION) {

    path=chooser.getSelectedFile().getAbsolutePath();

}

I have written this code in Servlet's doGet method.when i deployed in linux server getting following exception:

java.awt.HeadlessException
    at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)

Does anyone know how to over come this?

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
shiva tatikonda
  • 91
  • 2
  • 4
  • 14
  • 6
    What do you expect to happen? The servlet container doesn't have a graphical user interface, how should it show the `JFileChooser`? – mthmulders Mar 27 '13 at 12:15
  • When doGet executes one popup(JDialog) has to appear to choose the directory then i am retrieving that chosen directory in string. is it clear? But it is giving Headless exception at JDialog dialog=new JDialog();. – shiva tatikonda Mar 27 '13 at 12:27
  • 2
    Since the servlet container doesn't have a graphical user interface, it cannot display the `JDialog`. It tells you so by throwing the `HeadlessException`. A servlet usually just processes an HTTP request, and it depend on any user intervention on the server side. Read [Wikipedia](http://en.wikipedia.org/wiki/Java_Servlet) or [this article](http://www.onjava.com/pub/a/onjava/2003/05/14/java_webserver.html) for more information on how a servlet works. – mthmulders Mar 27 '13 at 12:28
  • Then what is the solution? Can i get the directory path (choosen by user) using javascript? – shiva tatikonda Mar 27 '13 at 12:32
  • 1
    also the java documentation on the Headless exception is self explanatory when it comes to trying to run GUI based applications on a server http://docs.oracle.com/javase/6/docs/api/java/awt/HeadlessException.html – dinukadev Mar 27 '13 at 12:32
  • I just need to generate a popup to select the folder path and need to store that path in string. can you please help how to do this? – shiva tatikonda Mar 27 '13 at 13:27

5 Answers5

5

HeadlessException

Thrown when code that is dependent on a keyboard, display, or mouse is called in an environment that does not support a keyboard, display, or mouse.

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155
3

Your HTML form should have something like this:

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" />
</form>

Then, using Apache Commons FileUpload, you can process the uploaded file in your Servlet. See their User Guide and FAQ for more information.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
  • I just need to generate a popup to select the folder path and need to store that path in string. can you please help how to do this? – shiva tatikonda Mar 27 '13 at 13:27
  • 1
    This cannot be achieved on the server side of your web application. The browser, who renders HTML content sent by your web application, will handle it for you automatically. Did you read [the two links I supplied earlier](http://stackoverflow.com/questions/15658493/java-awt-headlessexception/15658927?noredirect=1#comment22222550_15658493)? – mthmulders Mar 27 '13 at 13:33
  • can i get the popup and the directory path the user have choosen? using Javascript.I have read ur links but i dont need that much complicated code.I just need to get the path the user have choosen. – shiva tatikonda Mar 27 '13 at 13:40
  • 1
    Simple answer: you **cannot** have your servlet generate a popup. That's because the servlet runs in an environment that doesn't have a graphical user interface (as pointed out before). The servlet processess incoming HTTP requests, and if those requests contain a file upload, you could process that. Once again, the servlet itself cannot generate a popup. – mthmulders Mar 27 '13 at 13:42
  • I need to generate popup when the button clicked in jsp page.anyhow this jsp having action as servlet.Finally I need when a user clicks on a button a popup has to be generated to select the folder only and i need that path in string? is it clear? – shiva tatikonda Mar 27 '13 at 13:51
  • 1
    It is completely clear, however, you're trying to program the popup at the wrong location. It's something the browser takes care of, not the server. Read [this tutorial](http://www.avajava.com/tutorials/lessons/how-do-i-upload-a-file-to-a-servlet.html) on file upload to a servlet. – mthmulders Mar 27 '13 at 13:59
  • in this example i can upload using servlet.but here i need the the path of the directory which is choosen by user.how to get it? – shiva tatikonda Mar 27 '13 at 14:35
  • See the [User Guide](http://commons.apache.org/proper/commons-fileupload/using.html#Processing_the_uploaded_items) for a couple of hints. – mthmulders Mar 27 '13 at 14:46
  • 1
    What are you trying to accomplish, Shiva? The path the user chooses (on the client machine) is useless on the server. What is the servlet supposed to do with it? – Frans Dec 19 '13 at 14:48
3

In Fedora and some other linux distros, only headless java runtime is installed by default. You may need to install the full java runtime:

sudo yum install java-1.8.0-openjdk

Source: https://github.com/chatty/chatty/issues/261#issuecomment-392228006

Ray Foss
  • 3,649
  • 3
  • 30
  • 31
1

Web Applications usually run on Back End servers which are headless. Better to avoid any GUI except the ones that run within an Applet.

shazin
  • 21,379
  • 3
  • 54
  • 71
-1

If you run this under Linux with low Permission you get this:

java.lang.RuntimeException: Failed to create component for 'frame' reason: java.awt.HeadlessException
    at Pp$_run_closure1.doCall(Pp2.groovy:17)
    at Pp.run(Pp2.groovy:15)
    at Pp$run.call(Unknown Source)
    at Pp.main(Pp2.groovy:12)
Caused by: java.awt.HeadlessException

you have to run it with the right permissions.

First try this with maximum Permissions:

sudo groovy MyClass.groovy

This should solve your Problem.

Akatum
  • 3,976
  • 2
  • 18
  • 25
MarioG
  • 1