Search code examples
javajbossseamear

How to get current EAR location programmatically with JBoss


Does anyone know how to get programmatically the absolute path in the filesystem for an EAR deployed in JBoss, from Java code within that same EAR?

I need this because I want to copy some files that are inside the EAR to another part of the filesystem, on deploy-time.

Thank you everyone!


Solution

  • I do this way.
    EAR has a service MyService, where I work with EAR contents:

    import org.jboss.system.ServiceControllerMBean;
    import org.jboss.system.ServiceMBeanSupport;
    
    public class MyService extends ServiceMBeanSupport {
    
        public void workWithEar() 
        {
            ServiceControllerMBean serviceController = (ServiceControllerMBean) MBeanProxy.get(
                        ServiceControllerMBean.class,
                        ServiceControllerMBean.OBJECT_NAME, server);
            // server is ServiceMBeanSupport member
    
            ClassLoader cl = serviceController.getClass().getClassLoader();
    
            String path = cl.getResource("META-INF/jboss-service.xml").getPath()
            InputStream file = cl.getResourceAsStream("META-INF/jboss-service.xml");
        }
    }