I'm running an application from within the Tomcat container. The user clicks a link and this ultimately causes a method in a helper class to create a file and save it to the file system. When this code is run from a unit test / eclipse it saves the file in the applications root directory but when this is run from the browser / in Tomcat the file is stored in Tomcat's bin directory.
How can I find the applications root so I can choose where the save the file from there?
I need to programatically find the root so this can be deployed onto other environments running tomcat.
Thanks
You should not rely on the position of your Tomcat installation, and your application should not write into the Tomcat installation folder, never. It is quite possible that the Tomcat installation is not even writable for the user running Tomcat in a production environment.
Therefore, use full (absolute) paths only.
To avoid file permission issues you may want to use the user's home directory using System.getProperty("user.home"). That way it will work consistently across environments and operating systems.
Related
I'm trying to unzip in a directory symlinked in my WebApp and get this error
java.io.FileNotFoundException: /var/lib/tomcat/webapps/MyApp/_Apps/xxxxx.txt (Permission denied)
_Apps is my symlink (owned by tomcat, 777) and refers to a directory owned by tomcat (755)
My tomcat version is 7
I've put allowLinking="true" in my app context tag (plus override="true").
Any idea of what is going wrong?
I see several issues here, some related to your question, some to best practice:
You shouldn't upload any files to your webapp at runtime: They'll get overwritten with the next deployment (and will be lost), plus you open yourself up to uploads like attack.jsp - containing code that might end up running server side, under the account that tomcat is running as
Really, you should write to a different directory
That will make your issue go away.
In order to temporarily check what causes the issue:
Validate which user tomcat is running as
Validate that this user account has write access to the directory - to be more confident: Validate this through the linked directory and through the underlying directory, natively without using the link. (You can sudo -u tomcat /bin/bash to manually try)
Check, which file system the directory is on (it might be mounted read-only)
Once you've finished validating (or even before that, reluctant that you can work on a higher priority - security relevant - issue): Go back to the beginning of the list and change your implementation. Upload to a different folder.
We have tomcat 7.0.55 in our unix server. We have deployed web applications manually inside webapps folder of tomcat instance. By default the clusterinfo.1200.properties and log4j logs are stored in .businessobjects folder under home directory of the user account in which tomcat runs.
While accessing the InfoView web application we get the below error,
"AccessControlException:"java.io.filePermission:Access Denied \home\<user account of tomcat>\.businessobjects\clusterinfo.properties"
Tomcat 7 has restriction to access the file outside the tomcat directory on the first logon. However on refreshing the page we are able to access the application.
I have tried the following steps.
stopped the tomcat instance
Moved .buisnessobjects from home directory to inside tomcat installed directory.
Created symlink as .businessobjects in home directory to point to the folder inside tomcat directory.
Started the tomcat instance.
It works. But I need to know where the configuration change has to be made in web application to place the clusterinfo property file and log4j file inside tomcat directory.
I am a novice in Java. Please let me know the file and its location to make this changes.
Desperately looking for a solution :-(
Did you try to grant more permissions on this file with chmod.
The exception explicitly says that its java.io.filePermission. So the problem is that jvm can't read your properties file because your OS prevents it from doing so based on the file permissions.
You can see file permissions with ls -l filepath.
I vaguely remember that you had to change owner of the accesed files in order for tomcat to work so chown tomcat7 \home\<user account of tomcat>\.businessobjects\clusterinfo.properties wouldn't hurt, but I don't remember if it was only applicable to deployed artifacts like *.war files or such.
I've installed Eclipse with web development and during download have installed Tomcat7 to dir E:\Eclipse\tomcat7.
When in Eclipse and trying to test the program I'm getting a realPath of
C:/Users/user/Documents/eclipseJEEWorkspace/javaWebPages/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/org.example.web/WEB-INF/classes/
as my real path and not what I expect which is E:\Eclipse\tomcat7\webapps\org.example.web/WEB-INF/classes/
1) how do I get the real path of the servlet rather than the temporary path...
or am I missing something with how Eclipse uses the installed server? If this is the way then do I have to continuously create the WAR and fiddle around recreating/deleting all the time?
The reason for this is I'm also trying to get a JAVA Servlet getting access to a file in a path higher than webapps. I'm new to servlets....
My development includes third party software that all reference an individual file our.properties so changing the structure is unfortunately not a option.
My directory structure is:
/tomcat
/mycompany
/properties
our.properties //the file we want to access
/*otherfiles
/html
/*not used in this context but to show usage
/javascript
/*not used in this context but to show usage
/webapps
/org.example01.web
/META-INF
/WEB-INF
/org.example02.web
/META-INF
/WEB-INF
/org.example03.web
/META-INF
/WEB-INF
How do I tell my Servlet to access the our.properties file?
I've tried getPath, getResource but without getting the first bit to work, I doubt I'll be getting anywhere fast.
Thanks
When you run Tomcat under Eclipse, via the Tomcat plugin, the webapps path is changed. Eclipse uses .metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ folder to deploy the project. That's not a temporary path, it's the actual path since your web application is deployed there.
If you run tomcat from command line, or as a service and deploy your web application, you'll see that the path of the servlet will be as you expected.
In order the change the default path to deploy for Eclipse, double click to your Tomcat under Servers view and modify Server locations section.
We currently have an Java application that can be deployed on clients or run as a shortcut from the server. We have intermittently received ClassNotFound exceptions when running the JAR from the server which looks like Windows dropping the network connection with only part of the classes from the JAR loaded (user opens a different screen then the problem is apparent).
I am currently looking at the Web Start technology to allow us to run a single shortcut.
However the application has several folders it requires to be in the same folder the JAR is launched (for configuration, logs, etc.). These folders will require full access for all users to allow them to write log files. There are lots of configuration files under the configuration folder hierarchy.
The application also requires access to environment user settings (such as getting their user folder). It also requires certain command line parameters (including which folder to use for configuration, log file location, java memory usage, etc.)
Edit
The application also contains a reference to 2 signed JARs. These are:
jh.jar
mail.jar
It looks like these are signed by SUN Microsystems. jh is used for help integration in our application whilst mail is used for email integration.
I have now downloaded the latest versions of these files from java2s.com which do not have the signed equivalents.
end edit
The application is developed in Netbeans which compiles a single JAR file and copies the dependant JARs to the dist/lib folder. I enabled the Web Start functionality in Netbeans for the application and it generates the JNLP file.
Bearing everything in mind is Web Start the way to go for an application like this?
Can you include folders in a Web Start deployment? I could not find anything to do this in the XML structure for a JNLP file.
Cheers,
Andez
Yes, you could use WebStart for this.
Permission:
If you need permission to the file system, you have to sign your jars and put the all-permissions tag in your jnlp.
Arguments:
Arguments can be provided by using the arguments elements in application_desc.
Folders:
I'm don't think you can include folders in the WebStart application, but you could put the config files in a jar and read them from there, or extract them on startup.
ClassNotFoundError:
We have intermittently received
ClassNotFound exceptions when running
the JAR from the server which looks
like Windows dropping the network
connection with only part of the
classes from the JAR loaded
Does not sound plausible to me. per default, all jars will be downloaded before the application starts. If you set "download" to "lazy", the jar will be downloaded when first needed, but I would guess it will be downloaded completly then.
You can provide read-only resources for Java WebStart. Getting the log back is harder. I would suggest looking into a centralized logging solution, using one of the standard appenders in the log framework you use.
For a Java Web Start application it is always advised not to create files or folders in the class path. Recommend user's home directory to store your settings or database files.
I'm using Eclipse Java EE IDE and launch Tomcat from the Server's tab on Eclipse.
Where does Eclipse store generated servlet .java files for JSP files? I've checked the Tomcat installation directory, but nothing there.
Thanks.
Doubleclick the server entry in Servers view and check the path represented by Server path. Explore in there from inside the workspace directory. The generated classes are there.
If you choose the 2nd option Use Tomcat installation, then it will be available Tomcat's /work folder, there where you expected it to be.
go to your application work space directory(not eclipse installation directory) in windows explorer(not in eclipse IDE explorer).
in my laptop it is d:/applicationdata/j2ee
then follow the path as:
work space directory(in my case j2ee)/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/catalina/localhost
here u will find your application
Eclipse doesn't generate servlet files for JSP files. Validation happens directly on JSP syntax. When a JSP is deployed to an app server like Tomcat, the server may choose to generate servlet files to disk, but that is not required. The generation and compilation can happen in memory or the app server may even compile JSP files directly to bytecode.
I don't know specifically about Tomcat, but if it does generate servlet files to disk, the temporary directory containing these files will be somewhere under your Tomcat install.
/home/waheguru/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/Catalina/localhost/jspnotes/org/apache/jsp
localhost or your website name,
project name is jspnotes, whatever your project.
This path is shown in Linux Mint.
Check META-INF\context.xml file from your work space. There will be path metioned like below
workDir="C:\apache-tomcat-7.0.39\work"
In above path you will find the package structure of your project and in it there will be both *_jsp.java and *_jsp.class