Desktop.Desktop.getDesktop()。open(new File(path));给java.awt.HeadlessException

时间:2015-02-28 19:38:54

标签: java linux headless

您好我有一个Linux实例,我正在使用Desktop类。 代码:

 String path = request.getParameter("path");
           try {
               Desktop.getDesktop().open(new File(path));
               response.getWriter().write("<script>window.close();</script>");

           } catch (IOException ioe) {
                logger.error("doGet method of WordFileOpenerSample threw error:"+ioe.getMessage());
             ioe.printStackTrace();
          }

这在windows中工作得非常好。但是在linux中给出了java.awt.HeadlessException :. Linux是一个无头的环境。那么,现在我有什么选择,或者我如何在无头环境中使用Desktop类。

1 个答案:

答案 0 :(得分:0)

您可以考虑以下几点:

if (Desktop.isDesktopSupported())
{
        Desktop.getDesktop().browse(new URI(yourURL));
}
else
{
        Runtime runtime = Runtime.getRuntime();
        if (System.getenv("OS") != null && System.getenv("OS").contains("Windows"))
            runtime.exec("rundll32 url.dll,FileProtocolHandler " + yourURL);
        else
            runtime.exec("xdg-open " + yourURL);
}
相关问题