java 如何使用 JBoss 以编程方式获取当前 EAR 位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1234300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current EAR location programmatically with JBoss
提问by ptdev
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?
有谁知道如何从同一 EAR 中的 Java 代码中以编程方式获取在 JBoss 中部署的 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.
我需要这个是因为我想在部署时将 EAR 内的一些文件复制到文件系统的另一部分。
Thank you everyone!
谢谢大家!
采纳答案by avro
I do this way.
EAR has a service MyService, where I work with EAR contents:
我这样做。
EAR 有一个服务 MyService,我在其中处理 EAR 内容:
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");
}
}
回答by douarbou
you can do you "System.getProperty()" here is the linkfor other the properties you can used
你可以做“System.getProperty()”这里是你可以使用的其他属性的链接
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/..."
之后你只需要添加 "/deployments/YOUR_PROJECT_EAR/..."
回答by sylvain
To get the ServletContextfrom Seam, you can do:
要从ServletContextSeam获取,您可以执行以下操作:
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.
一旦 Seam 创建了applicationContext. 然后getRealPath("/")适用于根上下文的部署文件夹。可以访问上下文根中的任何文件夹结构。
回答by skaffman
This is quite fiddly, but you can do this by querying the JBoss MainDeployerMBean. The MBean is found at jboss.system:service=MainDeployer, and has a JMX operation listDeployments. This returns a collection of DeploymentInfoobjects, one of which will be your EAR deployment. That DeploymentInfo has a urlproperty which is a file://URL describing your deployment directory.
这非常繁琐,但您可以通过查询 JBoss MainDeployerMBean 来实现。MBean 位于jboss.system:service=MainDeployer,并具有 JMX 操作listDeployments。这将返回一组DeploymentInfo对象,其中一个将是您的 EAR 部署。该 DeploymentInfo 有一个url属性,它是file://描述您的部署目录的URL。
Nice, eh? You can use the raw JMX API to do this, but Spring provides a much nicer mechanism, using a MBeanProxyFactoryBeanto expose an instance of MainDeployerMBean.
不错,嗯?您可以使用原始 JMX API 来执行此操作,但 Spring 提供了更好的机制,使用 aMBeanProxyFactoryBean来公开MainDeployerMBean.
I'd like to find a simpler way, but that's the best I've found so far.
我想找到一种更简单的方法,但这是迄今为止我找到的最好的方法。
回答by matt b
Are these resources mapped or made available under a web path (within a WAR)?
这些资源是否在 Web 路径下(在 WAR 内)映射或提供?
If so, you could attempt to use ServletContext.getRealPath()to translate the virtual path to the real/filesystem path.
如果是这样,您可以尝试使用ServletContext.getRealPath()将虚拟路径转换为真实/文件系统路径。

