PDA

View Full Version : path problem


popprem
13-03-2009, 07:59
Hi,

I'v really stuck with an issue. I have done a web program using struts framework. I have a plugin class there for which i have to pass a configuration file as parameter. That file is in webapps/Myapp/WEB-INF/ directory. So i mentioned relative path as ../webapps/Myapp/WEB-INF/conf.xml. it works fine if i download tomcat zip file & extract it & use. Bt if i download a .exe file (eg: Apache-tomcat-5.5.15.exe) & run it to install & use it, struts doesnt get that path.

I'm really stuck with this prob. i need a way to specify path which works for both tomcat installations.

Please help me to solve this prob.

Note:
I thought to check where a file creates if i create a file from my program. When the tomcat.zip is used i found the created test file is in bin directory of tomcat. That means tomcat defaultly points to bin directory, so the path to the config file ../webapps/Myapp/WEB-INF/conf.xml. works fine.
When i used tomcat.exe & installed tomcat in my machine & run the program,i found that the test file i created in my code level was in windows/system32/ directory. so in this case tomcat doesnt defaultly points to the bin directory i guess.

hope this helps to give me a good answer.
Thanks in advance.

Kees de Kooter
13-03-2009, 09:32
It always tricky to put relative file system paths in your application.

Is the plugin able to find this conf.xml if you put it in WEB-INF/classes and set the path to "/conf.xml"?

If not please let me know what plugin this is about.

popprem
13-03-2009, 11:21
thanks very much for your early reply.

I tried it, bt it didnt work.

to explain more :
My struts-config.xml file is in /WEB-INF directory. I specify the plugin class & the parameter to that class (conf.xml) in this struts-config.xml. plugin class is in a jar which is in WEB-INF/lib.

if i summarize this :-
Inside WEB-INF directory i have classes,lib,struts-config.xml & conf.xml. i have to specify a plugin class & its parameter (conf.xml) in struts-config.xml file. That plugin class is in a jar which is in lib folder.

After your reply i copied the conf.xml to classes folder &specified the path as /conf.xml, it didnt work. also i copied that file to the lib folder (cz plugin class is in a jar inside lib) & tried the same thing, bt no use.

I thk the problem is more clear to you nw.
Pls help me with this issue.
Thanks in advance.

Kees de Kooter
13-03-2009, 11:37
Is it a plugin you wrote yourself? In that case please post the code that is loading the config.

popprem
13-03-2009, 12:24
this is the part which i specified in struts-config.xml :

<plug-in className="com.rcfi.qxlink.fixadapter.server.util.TFEInit">

<set-property property="configFile" value="../webapps/fixadaptorwebui-0.0.1/WEB-INF/conf.xml"/>

This works fine if i download tomcat.zip file, extract it & use it.
The problem comes when i use tomcat.exe file to install tomcat & run my application.

Kees de Kooter
13-03-2009, 12:37
Do you have the source code for com.rcfi.qxlink.fixadapter.server.util. TFEInit?

popprem
13-03-2009, 12:51
public class TFEInit extends HttpServlet implements PlugIn {

private String configFile;

// This method will be called at the time of application shutdown
public void destroy() {
}

// This method will be called at the time of application startup
public void init(ActionServlet actionServlet, ModuleConfig config)
throws ServletException {

System.setProperty("CONFIG", getConfigFile());



public String getConfigFile() {
return configFile;
}

public void setConfigFile(String configFile) {
this.configFile = configFile;
}

}

popprem
13-03-2009, 12:53
I have posted the source file.

I'm confused because it works with tomcat.zip bt nt with tomcat.exe.

Thanks in advance.

kjkoster
13-03-2009, 13:16
Dear popprem,

What you are running into is that Tomcat uses a different work directory (http://en.wikipedia.org/wiki/Working_directory) in each case. If you are not familiar with the concept, each application has a directory that it works from. The command line makes this very clear by displaying the work directory as the command line output. So if you work in the Windows temp dir, the prompt will show "C:\TEMP >".

If not changed, this is the directory that you or the operating system started the application from. The unzipped Tomcat you started in some place using the command line, but the service was started by the OS and it used its current work dir, which seems to be "windows/system32/".

By the way, I am amazed that you have write permissions in such an important directory, but that is an entirely different discussion. :)

You have to be very careful in web applications not to rely on the application server's work directory (as you are finding out). I work around this by using only paths configured using system properties and I configure them as absolute paths.

My advise to you is to try and see if absolute paths work in the config file, thus making the current work dir irrelevant.

Kees Jan

PS. To find out the Java application's current workdir, look at the system property named "user.dir" (http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperties()).

popprem
13-03-2009, 14:29
Thank you very much kees.

I understood wts going on.

The problem is i'm working on a product. Its has a .war file which the user should copy to his webapps directory of tomcat installation. Thats why i couldnt specify any absolute paths.

so, now i guess it is a must to ask the user for the tomcat installation path. correct?
do u have any solutions for this? i mean, without asking user about the path, how can i handle this problem?

Thankyou very much for all this support you gave me. I really understood the problem.

Kees de Kooter
13-03-2009, 14:32
You could try to avoid using filesystem paths (be it relative or absolute) altogether. Instead you could package the config file in the war and let the plugin pick it up from the classpath.

kjkoster
14-03-2009, 12:53
Dear popprem,

Glad to be of help.

Another solution might be to base finding files off of the real path from the servlet context (http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)) .

Kees Jan