my dog learned polymorphism*
The moose likes JSF and the fly likes Being Headless in java Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Being Headless in java Exception" Watch "Being Headless in java Exception" New topic
Author

Being Headless in java Exception

Rafael Campos Cruz
Ranch Hand

Joined: Apr 16, 2013
Posts: 38
Hi everyone! I'm having a little issue of java.awt.HeadlessException.

What is my situation: I have a application running on a server and I have certain methods wich use system based classes as Toolkit (in order to get the resolution from the screen of the user)
or JFileChooser (in for the user save a certain file to an specific folder) but bit these methods generate the java.awt.HeadlessException error.

How do I make the user the "head" of the session? Where should I start?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

If the server is "headless" that means it doesn't have a display attached to it, basically. So I don't understand how you plan to use a JFileChooser to ask the user something, when there isn't any display for it to appear on.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

This question doesn't even begin to work for JSF or for that matter for any other web technology. Web clients and servers are not bound together in a way that would permit the server to root around in the client's hardware configuration. Or even the user's filesystem.

A client web page can use JavaScript to obtain the dimension of the window that contains it and send them down to the server as part of a client request, but that's about as close as you can get.


Customer surveys are for companies who didn't pay proper attention to begin with.
Rafael Campos Cruz
Ranch Hand

Joined: Apr 16, 2013
Posts: 38
And how about getting a path to save the said file, how would I do that?
How would I pop up a a window or host some kind of interface to do so(I was aiming for something in the way of the "Save to.." window in windows, that's what the JFileChooser was doing, anyway) ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

I still don't understand the question. I suppose you mean you want to save a file on the server, is that correct? If so then why would it be up to the user to decide where to save it? At best the user won't have any idea about the server's file system, and at worst the user will be able to wipe out important files by (accidentally or deliberately) choosing a directory they shouldn't use.

This whole question is the wrong question. I think you need to go back to your design and rework it. Perhaps you could explain what your design is and why you think it involves having the user choose directories on the server.
Rafael Campos Cruz
Ranch Hand

Joined: Apr 16, 2013
Posts: 38
I think I'm just having a hard time to express myself XD

The application generates a .doc file for the user and I want him to be able to choose WHERE the said file is going to be saved (on his machine).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

Of course, that's perfectly normal. That's just an ordinary file download servlet. All you do is to write the contents of the file to the response's output stream and write response headers which say what kind of file it is. The browser will recognize those headers and help the client to save or display the document.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Ah. NOW I comprehend!

You have only a limited amount of control over that. What you need is to include a header that will suggest where to store the data. Something like this:


You still must use a servlet (or JSP) to generate the spreadsheet. JSF will mangle your output.

I don't guarantee that you can put a file path on the Content-Disposition and have it work. The "Filename" is only a suggestion made by the content to the user's browser for use in pre-populating the "Save As" dialog box that should appear when the data comes down. The server cannot assume anything about the client's local filesystem arrangement. For example, My Linux box has no such thing as a "C:" drive.
Rafael Campos Cruz
Ranch Hand

Joined: Apr 16, 2013
Posts: 38
Guys, I'm back again =/

So, I learned a little of javascript and managed to set the webpage visual-related data with it, solving at least part of the problem.

But now I'm having a really hard time on getting the file to the user!

So far I've learned that I can either:

1-) do it with jquery

2-) send the file location (on the server) to the user, for him to download it.

About the 1th option I have the following doubts, I would like to note that in order to generate the .docx file I'm using XWPF lib and that I'm trying to follow this [url = http://muaz-khan.blogspot.com.br/2012/10/save-files-on-disk-using-javascript-or.html ]guide[/url] in order to do the said "save to" thing:

a) By following this aprouch how should I link the file to the user? I mean (and please tell me right away if I'm being unclear) I create the object on my ManagedBean, so how can I get it to the javascript method "saveFileTo"?

b) Can I do it without creating the file on my system, just using the created object?

About the 2nd aprouch I really, really have no idea on how I would be able to do it! I tried creating the file and using the location on my system as its reference but it didn't work, so my guess is that I'm being remotelly wrong on this matter...

a') How do you create downloadable links for a file in the server?

b') Where the file should be stored? Any place is good?

Please, if I've been too plain on any part of this tell me that I'll ASAP clear out any point.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

That mostly looks wrong to me, but I don't really know what you're trying to do.

First of all you don't need any Javascript at all to write a file download servlet. Here's a file download servlet tutorial, you should be able to adapt that to some code which generates a docx file and sends the result as the servlet's response. Notice that it isn't necessary to save the result of generating the docx file anywhere in the server file system, that's just a waste of time and disk space.

As for how to create a link for this servlet, since we're still trying to solve your second problem and you haven't described the first problem which made you ask this question, it's hard to say. Normally the link would contain parameters which described how to generate the docx file, and the download servlet would use those parameters to generate the file and return it. Perhaps you could describe your actual requirements for this download instead of asking questions about your preconceived ideas about the solution.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Paul is correct. A web server is not a file server. You cannot open a file on a web server from a client, all you can to is submit a URL to the web server from the client and save what came back as a file on the client.

Every URL response that comes back to a client has a data type. The client determines what to do with the response based on that data type. For best results, the response will include a header (Content-type) that indicates a MIME (Multipurpose Internet Mail Extensions, since it was first developed for email attachments) type. In the absence of a content-type header, or cases where the content-type applies to more than one format, the client may also examine the data itself. Based on the data (MIME) type, the client may: A) display the data, suitable formatted, in a browser window, B) Prompt with a "File save" dialog for a place to save the incoming data as a file or C) use an alternative display mechanism, usually by opening an application. Whether A, B, or C is selected is actually controllable to a certain degree. Browsers come with pre-set choices, but often the user can change those presets. Additionally, the Content-Disposition header in the server's response can be used to influence the client's choice.

JSF is intended to produce non-HTML output such as Excel forms, which is why you need something like the servlet that Paul suggested. You can use the JSF h:outputLink tag to reference that servlet's URL.

What you don't need for that job is is Java Swing, headless graphics, or JavaScript. Swing/headless graphics won't work on the client, and the file can download just fine using basic HTML without JavaScript.
Rafael Campos Cruz
Ranch Hand

Joined: Apr 16, 2013
Posts: 38
Hey guys! I'm back again!

First of all Tim and Paul, thankyou very much for giving some light on the matter, it really got me on the right track of searching stuff.
However I, and my newbie self on servlets, am having some trouble on launching the servlet itself, I believe that, at least, I configured both web.xml and the servlet correctly.

But I'm wrong as it isn't launching :D

Here is my web.xml configuration on the servlet:



I stored the class as follows:



and am summoning it on the browser as I'm supposed to(I think):

http://localhost:8080/noticiasWeb/baixar

and, at last, this is the code of my servlet:



The methods ext.limpa(File io) just cleans the "arquivoDeSaida.docx" of text.

What am I doing wrong? Can you guys give me another hand :)?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Two things. ".class" in your servlet-class web.xml is redundant. It cannot be anything BUT a class. Remove that part.

Also, you didn't actually include a "package servlets;" statement in your sample code, so if you don't have on in the source file, things will not work.
Rafael Campos Cruz
Ranch Hand

Joined: Apr 16, 2013
Posts: 38
A little more progress! (I think)

I'm getting the following error now:



And, on the browser:



Any ideas on what would that be?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Yes, you have managed to name the servlets/Downloader.java file to servlets/Downloader.class. You need the real class file there, not its source code.
Rafael Campos Cruz
Ranch Hand

Joined: Apr 16, 2013
Posts: 38
God damn.... I'm feeling stupid right now..

Well... I'm returning to the button press action the link "/baixar" who, so I expected, would call the "download file action"....

But it doesn't... is there a right way to call it?

If I access "http://localhost:8080/noticiasWeb/baixar" on the browser I get to download the file...

Shouldn't the download be prompted by calling it this way on the commandButton?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

The problem with using a commandButton to download is that commandButtons (and commandLinks) invoke a JSF backing bean method that is expected to run through the normal JSF lifecycle, including returning a View designated by the return value of the action method.

This is why downloading is better done using an outputLink control, which allows you to request a non-JSF URL. If you prefer a "button", CSS can be used to make the link look like a button control. Actually, I think there's supposed to be a JSF link tag that renders a URL link that looks like a button without user-added CSS, although I can't locate it this morning.
Rafael Campos Cruz
Ranch Hand

Joined: Apr 16, 2013
Posts: 38
Woohoo! Made a workaround in order to make it visual-friendly: I took the image of the button wich I was using initially and put it over the outputLink!



I have one last doubt and we can wrap this all up and, if I could, pay a beer for both of you:

As I'm using a resource on the machine, the "arquivoDeSaida.docx" isn't there the risk that 2 concurring processes will access the file at the same time and generate an error?

If so, is there any way to prevent it from happening? (or to get rid of the need of the file on the disk itself)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

You can genenerate an image "on the fly", although the problem with this approach is that it depends on what image generation code you are using. And you won't be able to accurately set the content-length header because most image formats don't have sizes that can be predicted in advance.

An alternative is to generate the image to a temporary file. The java.io.File class has a method that will create a temporary File object, which you can write the image to, use it as the source for image download, then delete the temporary file.

The URL for doing this would be a servlet URL, of course, not a static image resource URL.
Rafael Campos Cruz
Ranch Hand

Joined: Apr 16, 2013
Posts: 38
Guys, really, thank you so much for all the help! I really learned a lot in this simple thread!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Being Headless in java Exception
 
Similar Threads
Implementing Access Rights on Axis2 Web Service?
Headless Exception
jdk 1.5.0.22 problem
Making JDialog visible
Automated Regression Testing suite