简体   繁体   English

如何在Tomcat中获取应用程序路径(不是从Servlet上下文中获取)

[英]How to get Application Path in Tomcat (not from a Servlet context)

What I need is very simple, but the solutions posted here do not apply to my specific case. 我需要的非常简单,但是此处发布的解决方案不适用于我的特定情况。 It seems it's possible to get catalina.base and from that guess your applications directory by following /webapps/<application_name> . 似乎有可能获得catalina.base并通过遵循/webapps/<application_name>猜出您的应用程序目录。

As already pointed out in the question's title, I'm not in Servlet context here, but in a regular class. 正如问题标题中已经指出的那样,我不在这里的Servlet上下文中,而是在常规类中。

I think that works in most cases within a standard Tomcat installation. 我认为这在大多数情况下都可以在标准Tomcat安装中使用。 But from within an IDE, where the directories are all separated (source code, build, catalina base, etc) it doesn't really work. 但是,在IDE中,所有目录都是分开的(源代码,内部版本,catalina基础等),它实际上是行不通的。 In NetBeans, for example, catalina.base is NOT where the application is located. 例如,在NetBeans中, catalina.base应用程序所在的位置。

What I want is to be able to guess the application directory so as to guess it's log files subdir. 我想要的是能够猜测应用程序目录,以便猜测它的日志文件子目录。 So in my local environment (NetBeans) I have /webapps/App/logs and anywhere else I would also have /webapps/App/logs. 因此,在我的本地环境(NetBeans)中,我有/ webapps / App / logs,在其他任何地方,我也都有/ webapps / App / logs。

What I can't seem to find is a sure way to fetch "whateverdir". 我似乎找不到找到“ whateverdir”的肯定方法。 Any ideas? 有任何想法吗?

Try this: 尝试这个:

  1. Define a servlet that loads on startup. 定义一个在启动时加载的servlet。 In its init() method use the "catalina.base" approach to get the directory. 在其init()方法中,使用“ catalina.base”方法来获取目录。
  2. After you get the directory, save the value in a system property. 获取目录后,将值保存在系统属性中。
  3. Then in your non-servlet code, simply read the system property. 然后,在您的非servlet代码中,只需读取system属性。

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

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