简体   繁体   English

获取JAVA EE Server中已安装服务器的路径

[英]Get Path of installed Server in JAVA EE Server

I need the main path from my OS of my installed JAVA EE Server (Wildfly) on which is the web app currently running. 我需要安装的JAVA EE Server(Wildfly)的操作系统上的主路径,该路径是当前正在运行的Web应用程序。

So for example: If the JAVA EE Server (Wilfly) is installed at: C:/Software/myApp/myServer 因此,例如:如果JAVA EE服务器(Wilfly)安装在:C:/ Software / myApp / myServer

Than I need exactly this location. 比我确切需要这个位置。 How can I do this. 我怎样才能做到这一点。 It could be that there is another instance for Wildfly, so I cannot use the System variable. 可能是Wildfly还有另一个实例,所以我不能使用System变量。

Try System.getProperty("jboss.home.dir") . 试试System.getProperty("jboss.home.dir")

Anyway, depending on the installation location is a bit of a design smell. 无论如何,取决于安装位置是否有点设计异味。

Unfortunately your description of your question was not clearly, because EJB is business logic and not directly your web app, also when you deploy all within your war file. 不幸的是,您对问题的描述并不清楚,这是因为EJB是业务逻辑,而不是直接在您的Web应用程序中,并且在将所有内容都部署在war文件中时也是如此。 Because the EJB has no information about the request itself except it is a webservice with JAXWS. 因为EJB没有关于请求本身的信息,但它是带有JAXWS的Web服务。

When you only need the root path to your server and not the web app then you can use like @Harald Wellmann wrote : System.getProperty("jboss.home.dir") and you will get the root path of your server. 当您只需要服务器的根路径而不是Web应用程序的根路径时,可以使用@Harald Wellmann写道: System.getProperty("jboss.home.dir") ,您将获得服务器的根路径。

If you need the path to your app then you could use for example 如果您需要应用程序的路径,则可以使用例如

    this.getClass().getProtectionDomain().getCodeSource().getLocation()

which will give you as standard behavior the path to your /WEB-INF/classes folder. 这将作为标准行为为您提供/WEB-INF/classes文件夹的路径。

If you would use JSF, JSP or Servlets, then it is much simpler and you could get the information directly through the HttpServletRequest object with getServletContext().getRealPath("/") like I wrote before. 如果您将使用JSF,JSP或Servlet,那么它会更简单,并且可以像我之前写的那样,通过带有getServletContext().getRealPath("/")的HttpServletRequest对象直接获取信息。

And with an EJB with JAXWS you could inject get the HttpServletRequest object like this: 对于带有JAXWS的EJB,您可以像这样注入HttpServletRequest对象:

    @Context
    private HttpServletRequest httpRequest;

Path to server dir (server/bin): 服务器目录(服务器/ bin)的路径:

File inputFile = new File("file.xml"); // server_dir/bin/file.xml

Path to other server directories, "webapps" for example: 其他服务器目录的路径,例如“ webapps”:

File inputFile = new File("../webapps/file.xml"); // server_dir/webapps/file.xml

Good way to get rid of the absolute paths :) Tested on Tomcat, but hopefully it will help. 摆脱绝对路径的好方法:)在Tomcat上进行了测试,但希望它将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM