0

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?

Community
  • 1
  • 1
Peter Ilfrich
  • 3,727
  • 3
  • 31
  • 35

2 Answers2

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Andreas Veithen
  • 8,868
  • 3
  • 25
  • 28
0

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

Peter Ilfrich
  • 3,727
  • 3
  • 31
  • 35
  • That's not so true. The *working* directory only appears as the bin directory of tomcat because this is the place where you start tomcat from. Try running tomcat from another place on your system: for example: `cd ~; /path/to/tomcat/bin/startup.sh` The working directory will be your user home directory. – joker Nov 12 '16 at 18:23