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

混江龙づ霸主 提交于 2019-12-24 17:22:44

问题


I have a Java EE project that I will turn into a war file. 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. For example, my war file is:

C:\project.war

then my log file's path would be:

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?


回答1:


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. It may be deployed as a WAR archive or as an exploded directory. 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. 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...).

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.

  • http://www.nailedtothex.org/roller/kyle/entry/java-util-logging-programmatic-configuration (JDK Logger example)
  • http://www.tutorialspoint.com/log4j/log4j_sample_program.htm (Log4J example)

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:

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. Again, this is not a good idea, and I will leave it to you to decide if you should use this approach.




回答2:


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.

Declare the following in you Tomcat's context.xml file:

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

Get the logLocation using:

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);


来源:https://stackoverflow.com/questions/33634344/how-to-find-the-parent-directory-of-a-deployed-java-ee-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!