alci
alci

Reputation: 345

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. The class I want to run is within 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.

NB: This has to work in Tomcat or Jetty (or any decent servlet container).

Upvotes: 1

Views: 2193

Answers (1)

NoozNooz42
NoozNooz42

Reputation: 4328

Would getServletContext().getRealPath(...) be what you're looking for?

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). My webapp has got an "images" repository into which all my images are present. 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?

Upvotes: 1

Related Questions