I'm using Java Spring Boot and deploying to Tomcat 8.1 webapp.
When I create a new File() with a relative path, it is referring to the absolute root of the system.
For instance,
File x = new File("./temp.txt");
System.out.println("X Path: " + x.getAbsolutePath());
The output is
/./temp.txt
Why is this? I'm getting a Java.nio.File.AccessDeniedException because Tomcat does not have permissions to write to the root.
The code is a third party library creating a cache file, so I can't feasibly just change to absolute path.
What causes Tomcat to use / as my working directory?
So I determined the problem. Tomcat, when executed as a service (CentOS: systemctl start tomcat) the WorkingDirectory defaults to root if it is not set.
By editing the tomcat.service file in /etc/systemd/system and adding under the [Service] section the following:
WorkingDirectory=/opt/tomcat
I was able to move the directory where the third party library was writing.
Related
My .war file is created with a version in it. For example:
myproject-1.0.0-SNAPSHOT.war
For next version it would be second version :
myproject-2.0.0-SNAPSHOT.war
and so on.
Now if I deploy to tomcat the path would change with every new version as Tomcat just takes the filename as the context path.
Of course this is not acceptable as the path should be fixed.
So I already tried to create a context.xml file in META-INF folder and set path there , but did not help.
What else could I do ?
You can include a context version in your war's file name by using "##" as delimiter, see https://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Naming
It will be stripped on deployment, leaving you with the same context path as before.
You could rename the war file before deploying.
Or, if you're using the api from the manager app to deploy, set the path parameter
http://localhost:8080/manager/text/deploy?path=/foo
Reference: https://tomcat.apache.org/tomcat-8.5-doc/manager-howto.html
I just started learning Java EE and came across this error when attempting to deploy a WAR file on my tomcat server.
This error happens every time I use the manager application to deploy a WAR. When I select the file located at C:\Users\julex_000\workspace\application\target\application-0.0.1-SNAPSHOT, Tomcat sets it's name as /C:Usersjulex_000workspaceapplicationtargetapplication-0.0.1-SNAPSHOT. It then creates a file called C in the Tomcat8.0\webapps folder and when I try to start the application it fails every time.
The invalid ':' seems to be the colon after C in the path C:\Users\...
I also get an IOException about this:
This error only occurs when using the manager application. When I copy and paste the WAR into the webapps directory it works just fine.
It is simply not allowed to have a : in the context path.
At the deployment you have to pass a valid context path.
For example if you are using the tomcat manager ui:
You have to set the field Context Path and the WAR or Directory URL (the lokal path on the application server where tomcat can find the war file; If you tomcat is not on your localhost you have to move the war file manually and use the path on the server).
If you are using the form with the upload field in the ui, there is no input field where you can pass the context path, but tomcat will use the filename of the war (not the complete path) as context path.
At last but not least if you are using the non-ui-api you can just send your war file with a http put request and pass the url parameter path. You can find more information about this in the Documentation.
I have a Jersey based Rest service running on a tomcat server. There is no UI, just a server that offers some rest services. Now, to access this service the URL that i have to type in is pretty long. Something like localhost:8080/MyApp/url_pattern/classPath/method where MyApp is the webapp that i deployed, url_pattern is the pattern that i defined in the servlet-mapping in web.xml, classPath and method being the #Path annotations for the Class and method respectively. Is it possible to shorten it such that I get rid of the MyApp and url_pattern part of this URL. Something like localhost:8080/classPath/method.
PS: There is just one webApp running on this server, so no point having the MyApp part
I don't think you can remove all what you desire from the url but you can definitely remove the MyApp part by making it the root application for tomcat.
Answer on this related link describes it pretty well, how to set your application as the root application. So you can access your REST services without having the app name in url:
Setting default application in tomcat 7
Content copied from the above link:
First Method:
first shutdown your tomcat [from the bin directory (sh shutdown.sh)]
then you must delete all the content of your tomcat webapps folder (rm
-fr *) then rename your WAR file to ROOT.war finally start your tomcat [from the bin directory (sh startup.sh)]
Second Method:
leave your war file in CATALINA_BASE/webapps, under its original name
- turn off autoDeploy and deployOnStartup in your Host element in the server.xml file. explicitly define all application Contexts in
server.xml, specifying both path and docBase. You must do this,
because you have disabled all the Tomcat auto-deploy mechanisms, and
Tomcat will not deploy your applications anymore unless it finds their
Context in the server.xml.
Note:
that this last method also implies that in order to make any change to
any application, you will have to stop and restart Tomcat.
Third Method:
Place your war file outside of CATALINA_BASE/webapps (it must be
outside to prevent double deployment). - Place a context file named
ROOT.xml in CATALINA_BASE/conf//. The single element in this context
file MUST have a docBase attribute pointing to the location of your
war file. The path element should not be set - it is derived from the
name of the .xml file, in this case ROOT.xml. See the Context
Container above for details.
Hi i must create a XML file in the project using java dynamically and must read it using the base Path URL. i can read the File when i create it manually, but not able to create it dynamically. When i use File f1 = new File("test.XML"); it create the file in the tomcat's Bin folder. The file must be created in the Project while Running in both Tomcat and Jboss EAP 6.
You can get the real path of the web application using the servlet context:
new File( servletContext.getRealPath( "/text.XML" ) );
Note: if you're running tomcat/jboss on Linux, you'll probably have to give permission to write in the webapp folder though, which is most likely forbidden by default.
Quoting from Servlet API docs of ServletContext.getRealPath():
Returns a String containing the real path for a given virtual path.
For example, the path "/index.html" returns the absolute file path on
the server's filesystem would be served by a request for
"http://host/contextPath/index.html", where contextPath is the context
path of this ServletContext..
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?
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.
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