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
Related
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.
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 need to link an external folder located in : Tomcat_8/folder to an application I have located in : Tomcat_8/wtpwebapps/APP
I tried adding a context in server.xml :
<Context docBase="/Users/Mac/Documents/apache-tomcat-8.0.32/folder" path="/APP/images"/>
It works but tomcat keeps deleting this line and I read that its not a good idea to place contexts inside server.xml.
I added the same line in :
context.xml
conf/catalina/localhost/ROOT.xml
conf/catalina/localhost/myapp#images.xml
conf/catalina/localhost/myapp.xml
APP_FOLDER/WEB_INF/context.xml
Nothing works at all except the server.xml one.
It depends on what exactly you want to do with the folder actually. One way is do write a script that is called setenv.sh/setenv.bat and place it in the bin directory of Tomcat. The content of that script would be for example definition of environment variables for the path/file(s) you want to be available to the application. You do not need to change anything else - Tomcat startup script by default will invoke the file called setenv.sh/setenv.bat in the bin directory if it exists there. Let's say you define the directory MYPATH =/APP/images in your setenv.sh and then you can access the directory from your app using the MYPATH variable that is avaialble to the Tomcat instance while running.
I am not sure how context path is set.
When I rename my .war file in tomcat on autodeploy, the web page goes to localhost:8080/newDirectory as expected, however for some reason wherever there's a call to pageContext.request.contextPath in a Spring based page, it still returns the old context path.
I tried to override the context path by setting:
<context path="/newDirectory" docBase="appName" override="true"></context>
in server.xml but it doesn't work.
My question is, where does Spring read its context path from? I used Maven and I did see there's a
<appContext>/${project.artifactId}</appContext>
in the pom.xml, does this mean I need to rename the artifactId to newDirectory ?
I have also tried adding that <context path="/newDirectory"...> in /META-INF/context.xml (which now I know will be ignored anyway due to my server.xml changes).
Thanks in advance for your answer.
It not depends on Spring maybe you are using a maven plugin to build your war that reads the appContext property. You can read about definig ServletContext in this thread.
Every PageRequest will get current HttpServletRequest object and get context path of current request and append your .jsp (that will work even if the context-path this resource is accessed at changes).
Eg,
if you have war file as MyCompany.war and having a page with ${pageContext.request.contextPath}/MyJspPage.jsp.
Then your context path is http://abc/MyCompany and it works as http://abc/MyCompany/MyJspPage.jsp.
Suppose if you change your war file as OurCompany.war,
then your context path changes to http://abc/OurCompany and Jsp will work as http://abc/OurCompany/MyJspPage.jsp.
It means context path will change automatically to name of the application(War file name) with out any changes.
In your case,after renaming war file name with newDirectory,your webserver will deploy newDirectory application but still newDirectory application exist in the web server.I think you should delete old application from webapp and then check by reloading your newDirectory application.
I'm working in maven web application. I need to read a directory(For ex: Files) in my webapp folder as follows,
Java.io.File file = new Java.io.File("path");
But I don't know how to specify the path of the directory here.
You shouldn't give local path addresses. Path should be a relative address, e.g. /files/images under your web archive (.war) folder.
To use relative paths properly, I suggest you to add your target folder to the resources definiton of POM.xml, check out these pages
http://www.mkyong.com/maven/how-to-change-maven-resources-folder-location/
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
You can refer to resources folder easily with something like this:
this.class.getResource("Mydirectory/SubDirectory");
When in doubt how relatives paths work, it's always best to do something like that:
System.out.println(new File("/my/desired/directory").getAbsolutePath());
This will print out the path in which classpath will look for the files.
Assuming:
servlet container webapps dir is located in: /var/lib/tomcat6/webapps
your webapp is called my-webapp.war
You should see the following output: /var/lib/tomcat6/webapps/my-webapp/my/desired/directory
Another pointer: you have mentioned that you are looking for webapp directory. I hope you know that this directory will not end up in *.war - it's contents will.
War files are not always expanded when they are deployed to an app server, so it's possible that a relative path won't exist in a filesystem at all.
Best bets are to use getResource from the class loader, which will return things in the class path (the WEB-INF/lib directory, etc), or to use the getResource() method of ServletContext to find things in the web application itself.