I’ve a normal Java class (neither a JSP nor a Servlet) that performs some processing on files like reading, writing, and executing some executable files. How to know the relative path of each file?
I know about getClass().getResourceStream(), but this is useful only for reading files. And I know about getServletContext().getRealPath() but my code is in ordinary Java class not a servlet.
I’d like to do the following
String relativePath = "path/to/exe";
Runtime.getRuntime.exec(relativePath);
Just pass its result to the Java class.
Note that the
getRealPath()will returnnullwhen the deployed WAR is not expanded in local disk file system, but instead in memory. Some production server configurations will do that.Much better is to make the location of the exe file a configuration file setting. E.g.
This configuration file can in turn be placed in the classpath, or its path can be added to the classpath. See also Where to place and how to read configuration resource files in servlet based application?