简体   繁体   English

如何找到已部署的 Java EE 应用程序的父目录?

[英]How to find the parent directory of a deployed Java EE application?

I have a Java EE project that I will turn into a war file.我有一个 Java EE 项目,我将把它变成一个 war 文件。 When it runs, it will generate a log file, I would like the file to be generated at the same parent directory as the war file.当它运行时,它会生成一个日志文件,我希望该文件与war文件在同一父目录下生成。 For example, my war file is:例如,我的war文件是:

C:\\project.war C:\\project.war

then my log file's path would be:那么我的日志文件的路径将是:

C:\\log.txt C:\\log.txt

But all I found was how to find resources inside the project.但我发现的只是如何在项目中查找资源。 How can I find the parent directory of my project before it generates a log file?如何在生成日志文件之前找到我的项目的父目录?

You should not rely on paths like this.你不应该依赖这样的路径。 The WAR that you create may get deployed on a completely different path when you distribute it to someone for deployment.当您将其分发给某人进行部署时,您创建的 WAR 可能会部署在完全不同的路径上。 It may be deployed as a WAR archive or as an exploded directory.它可以部署为 WAR 存档或展开的目录。 The target app server (Tomcat, JBoss, WebLogic etc.) may do it differently and making implicit assumptions like log file path relative to the WAR deployment path will not work outside of your immediate development environment.目标应用程序服务器(Tomcat、JBoss、WebLogic 等)可能会以不同的方式执行此操作,并且进行隐式假设(例如相对于 WAR 部署路径的日志文件路径)在您的直接开发环境之外将不起作用。 Plus, Java EE will not give you access to the physical location of the WAR's deployment directory, simply because it does not know about it (again, because how the WAR is deployed is not part of Java EE specs, but rather left to the implementers such as Tomcat, JBoss...).此外,Java EE的不会给你访问到WAR的部署目录的物理位置,只是因为它不知道它(同样,因为是如何部署的WAR不是Java EE规范的一部分,而是有待实现例如Tomcat、JBoss...)。

The correct approach, in my opinion, would be to create a properties file that you can keep in the classpath, and put a property of the log file directory in the properties file that can be read by the application at runtime and emit the logs to that file.在我看来,正确的方法是创建一个可以保留在类路径中的属性文件,然后将日志文件目录的属性放入应用程序在运行时可以读取的属性文件中,并将日志发送到那个文件。 See Reading Properties file for some more insights.有关更多见解,请参阅阅读属性文件

    Properties props = ...; // the link above will help you get the properties
    String logDir = props.getProperty("log.dir", "/tmp"); // /tmp is default

    // logDir cannot be null
    File logFile = new File(logDir, "application.log"); 

    // make sure the directories are created if did not exist previously
    logFile.getParent().mkdirs(); 

    // write log statements to logFile
    ...

Alternatively, and specifically for logging, you can take a look at the out of the box mechanisms that many APIs provide (including JDK) to log.或者,特别是对于日志记录,您可以查看许多 API(包括 JDK)提供的开箱即用的日志记录机制。

And of course, if you really need to work with physical directories, you can possibly do something like this to get access to the directory from where the application server (which deploys the WAR file) is started:当然,如果你真的需要处理物理目录,你可以做这样的事情来访问应用程序服务器(部署 WAR 文件)启动的目录:

File f = new File("."); // f is the current directory; where the JVM was launched
System.out.println(f.getAbsolutePath()); 

Once you have 'f', you can derive other paths off of it.一旦有了“f”,就可以从中导出其他路径。 Again, this is not a good idea, and I will leave it to you to decide if you should use this approach.同样,这不是一个好主意,我会让你决定是否应该使用这种方法。

Instead of trying to get a file relative to the application, try using an absolute path to refer the file.不要尝试获取相对于应用程序的文件,而是尝试使用绝对路径来引用文件。 For a cross platform solution, use JNDI.对于跨平台解决方案,请使用 JNDI。

Declare the following in you Tomcat's context.xml file:在 Tomcat 的context.xml文件中声明以下内容:

<Context>
    <Environment name="logLocation" value="C:/logs/log.txt" type="java.lang.String"/>
</Context>

Get the logLocation using:使用以下方法获取logLocation

InitialContext initialContext = new InitialContext(); 
Context environmentContext = (Context) initialContext.lookup("java:/comp/env");
String logLocation = (String) environmentContext.lookup("logLocation");

And then get your file:然后获取您的文件:

File file = new File(logLocation);

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

相关问题 如何从POJO获取Java EE 6应用程序中的上下文根目录? - How to get the context root directory in Java EE 6 application from a POJO? Java EE + Wildfly 9:使用已部署应用程序外部的目录进行上传 - Java EE + Wildfly 9: Use directory outside the deployed app for upload 如何找到Java EE Web应用程序的系统要求 - How to find the system requirements for java ee web application Java EE应用程序服务器如何找到类路径? - How does Java EE application server find the classpath? 加载/浸泡测试已部署的Java EE应用程序的工具/技术? - Tools/techniques to Load/Soak test a deployed Java EE application? 是否可以检索部署Java EE应用程序的端口 - Is it possible to retrieve the port where a Java EE application is deployed 如何使用Java查找应用程序安装路径目录 - How to find an application installation path directory with java 如何将外部目录结构添加到Java EE Web应用程序类路径 - How can I add an external directory structure to my Java EE web application classpath Java EE目录结构 - Java EE Directory Structure 使用在另一个应用程序服务器中部署的应用程序服务器Java EE jars编译的类 - Class compiled using an application server Java EE jars deployed in another application server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM