问题
So I have an Axis2 web service (called "ReleaseService"), which requires a properties file to work correctly. I've deployed axis2 into Tomcat7 on RHEL6 and have a structure like this:
tomcat/webapps/axis2
+ axis2-web, META-INF, org
+ WEB-INF
+ + classes, conf, lib, modules
+ + services
+ + + ReleaseService
+ + + + com, lib, META-INF
Thinking about Java, I would expect the working directory to be tomcat/webapps/axis2/WEB-INF/services/ReleaseService
, because it contains a lib folder and the root folder for my binaries.
So I put my properties file in there and tried to access it via
File configFile = new File("releaseservice.properties");
which apparently doesn't work. I've looked for hours but couldn't find a post or documentation snippet, which tells me where the working directory is. I found this, but a system property is no option for me, because I want to keep the deployment simple.
Found out, that the working directory is my tomcat/bin
folder, which is the root of the Tomcat Java process.
Bonus question: How can I find out my service directory inside my web service? Does Axis2 provide any helpers to find out which is the folder for the service?
回答1:
Making assumptions about the current working directory in application code deployed in a Java EE container is not recommended. In addition, you are making the assumption that when the WAR is deployed, releaseservice.properties
will exist as a file, i.e. that the container explodes the WAR. This is true for Tomcat, but may not be the case on other servers.
Axis2 creates a distinct class loader for every deployed service. This means that you can access your property file by looking it up as a resource:
MyService.class.getResourceAsStream("/releaseservice.properties")
Replace MyService
with the actual class implementing your service, or any other class loaded from WEB-INF/services/ReleaseService
.
回答2:
Apparently the working directory is the tomcat/bin
folder. Even though it seems like there is a dedicated application running inside tomcat, it's all the same Java process.
Found it out by adding the following debug code:
File test = new File("abc.txt");
System.out.println(test.getAbsolutePath());
Which returned /opt/tomcat/bin
来源:https://stackoverflow.com/questions/12511163/axis2-tomcat-web-service-working-directory