Twitter github

Am I Headless?

Through my daily browsing of the Eclipse newsgroups, I saw a post about how do detect whether a bundle is running headless. I had to do something similar several months ago, but I figure I would post a solution for people whoever come across this issue in their bundle-development lives.

The gist of the solution is to detect whether the SWT bundle is installed and running. Here’s a basic snippet on how to do this:

Bundle b = Platform.getBundle("org.eclipse.swt");
if (b==null || b.getState() != Bundle.ACTIVE) {
System.out.println("sweet, headless mode!");
}

If you don’t care for the dependency on the Platform class and the bundle it comes from, you can do a “pure OSGi” solution of using PackageAdmin. I recommend using getBundles(…) from PackageAdmin to get access to the SWT bundle to do a similar check.

That’s it… I’m not sure if a more elegant solution exists.

  • Eugene Kuleshov

    Hmm. What if one would want to render pictures and stuff from the headless?…

  • Eugene Kuleshov

    Hmm. What if one would want to render pictures and stuff from the headless?…

  • Markus

    What if the UI would have been written in Swing or some other UI toolkit?

  • Markus

    What if the UI would have been written in Swing or some other UI toolkit?

  • AlBlue

    That’s not necessarily going to work. A system can be headless but have dependencies on that bundle (or that bundle might be installed); or, alternatively, you might be using some other UI mechanism.

    For example, the Eclipse ‘helpserver’ application would result in your code thinking it was not headless, when in fact there’s no windows.

    In the RCP case, a better idea might be to detect whether there was a workbench window.

  • AlBlue

    That’s not necessarily going to work. A system can be headless but have dependencies on that bundle (or that bundle might be installed); or, alternatively, you might be using some other UI mechanism.For example, the Eclipse ‘helpserver’ application would result in your code thinking it was not headless, when in fact there’s no windows.In the RCP case, a better idea might be to detect whether there was a workbench window.

  • Martin Oberhuber

    Yes, using
    PlatformUI.isWorkbenchRunning()
    has been suggested as well to know if we are headless. The downside of this is that it’ll load the org.eclipse.ui plugin and all its dependencies for the mere purpose of knowing whether it’s headless or not.

    I guess that the really correct check might depend on what you want to accomplish in your headless application. I agree that some headless apps may want SWT for some tasks even if there is no display and no window. Feel free to replace “org.eclipse.swt” by “org.eclipse.ui” or any other bundle you don’t expect to be active when running headless and thus get your customized headless check.

    Also see the original newsgroup thread on http://dev.eclipse.org/newslists/news.eclipse.technology.equinox/threads.html#03391

  • Martin Oberhuber

    Yes, using PlatformUI.isWorkbenchRunning()has been suggested as well to know if we are headless. The downside of this is that it’ll load the org.eclipse.ui plugin and all its dependencies for the mere purpose of knowing whether it’s headless or not.I guess that the really correct check might depend on what you want to accomplish in your headless application. I agree that some headless apps may want SWT for some tasks even if there is no display and no window. Feel free to replace “org.eclipse.swt” by “org.eclipse.ui” or any other bundle you don’t expect to be active when running headless and thus get your customized headless check.Also see the original newsgroup thread on http://dev.eclipse.org/newslists/news.eclipse.technology.equinox/threads.html#03391

  • Martin Oberhuber

    For the sake of formatting:
    The original newsgroup thread is
    here.

  • Martin Oberhuber

    For the sake of formatting:The original newsgroup thread ishere.

  • Chris Aniszczyk (zx)

    That’s true Alex, there may be cases that this doesn’t work. PDE uses this snippet as sort of a “litmus test” in terms of regarding how to launch PDE JUnit tests.

    Good discussion though!

  • Chris Aniszczyk (zx)

    That’s true Alex, there may be cases that this doesn’t work. PDE uses this snippet as sort of a “litmus test” in terms of regarding how to launch PDE JUnit tests.Good discussion though!

  • Jacek Pospychala

    so maybe if you just combine Chris and Martin solution like this:

    Bundle b = Platform.getBundle(“org.eclipse.ui”);
    if ((b==null) || (b.getState() != Bundle.ACTIVE) || (! PlatformUI.isWorkbenchRunning())) {
    System.out.println(“sweet, headless mode!”);
    }

    then if platform.ui is active, you don’t loose anything checking isWorkbenchRunning(), because all PlatformUI prerequisites are already up and running.

  • Jacek Pospychala

    so maybe if you just combine Chris and Martin solution like this:Bundle b = Platform.getBundle(“org.eclipse.ui”);if ((b==null) || (b.getState() != Bundle.ACTIVE) || (! PlatformUI.isWorkbenchRunning())) { System.out.println(“sweet, headless mode!”);}then if platform.ui is active, you don’t loose anything checking isWorkbenchRunning(), because all PlatformUI prerequisites are already up and running.