How to get current EAR location programmatically with JBoss
Question
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");
}
}
OTHER TIPS
you can do you "System.getProperty()" here is the link for other the properties you can used
ex:
String jBossPath = System.getProperty("jboss.server.base.dir")
result
"/Users/ALL_THE_PATH/JBoss_7-1/standelone"
After you just need to add "/deployments/YOUR_PROJECT_EAR/..."
To get the ServletContext
from Seam, you can do:
ServletLifecycle.getCurrentServletContext()
which is available as soon as Seam has created the applicationContext
. And then getRealPath("/")
works fine for root context's deployment folder. Any folder structure within context root can be reached.
This is quite fiddly, but you can do this by querying the JBoss MainDeployer
MBean. The MBean is found at jboss.system:service=MainDeployer
, and has a JMX operation listDeployments
. This returns a collection of DeploymentInfo
objects, one of which will be your EAR deployment. That DeploymentInfo has a url
property which is a file://
URL describing your deployment directory.
Nice, eh? You can use the raw JMX API to do this, but Spring provides a much nicer mechanism, using a MBeanProxyFactoryBean
to expose an instance of MainDeployerMBean
.
I'd like to find a simpler way, but that's the best I've found so far.
Are these resources mapped or made available under a web path (within a WAR)?
If so, you could attempt to use ServletContext.getRealPath()
to translate the virtual path to the real/filesystem path.