简体   繁体   English

从servlet中获取Web应用程序类路径

[英]Obtaining the Web application classpath from within a servlet

The reason to do that is because I want to use Runtime.exec() using the same classpath as my servlet. 这样做的原因是因为我想使用与我的servlet相同的类路径来使用Runtime.exec()。 The class I want to run is within WEB-INF/classes/my/package/. 我要运行的类在WEB-INF / classes / my / package /中。

So I want to build a cmdarray as String[] {"java","-cp", my_servlet_classpatch, "my.package.myclass"} I just can't find a way to get my running servlet classpath. 因此,我想将cmdarray构建为String [] {“ java”,“-cp”,my_servlet_classpatch,“ my.package.myclass”}}我只是找不到一种方法来获取正在运行的servlet类路径。

NB: This has to work in Tomcat or Jetty (or any decent servlet container). 注意:这必须在Tomcat或Jetty(或任何合适的servlet容器)中工作。

Would getServletContext().getRealPath(...) be what you're looking for? 您正在寻找getServletContext().getRealPath(...)吗?

For example I need to do some image processing if the webapp is on Linux and if the ImageMagick tools are available (and, no, I don't want to use the Java ImageMagick wrapper). 例如,如果Web应用程序在Linux上并且ImageMagick工具可用,那么我需要进行一些图像处理(不,我不想使用Java ImageMagick包装器)。 My webapp has got an "images" repository into which all my images are present. 我的Web应用程序有一个“图像”存储库,其中包含我所有的图像。 I can get the actual deployed directory by issuing a: 我可以通过发出以下命令来获取实际的部署目录:

getServletContext().getRealPath( "images" )

Which gives: 这使:

/home/tomcat/apache-tomcat-6.0.26/webapps/mywebapp/download

Note that in your case I'm not sure you need this: do you really want to spawn a new Java process using Runtime.exec? 请注意,在您的情况下,我不确定您是否需要这样做:您是否真的要使用Runtime.exec生成新的Java进程?

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

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