1

I have written a java Servlet web application, using NetBeans 7.2.1. The program have some jar file libraries that I have attached to the project. The application runs fine using NetBeans and Apache Tomcat 7.0.27.

My problem is that some of the jar file libraries that I am using in the project, need to access to some folders and files. I put these folder and files on the same directory as the whole NetBeans project is. but I got this exception:

Exception: java.lang.RuntimeException: java.io.FileNotFoundException

So I used these codes to find out where should I put them:

out.println("current directory: " + new File(".").getAbsolutePath());
out.println("current directory: " + System.getProperty("user.dir"));
out.println("current directory: " + getServletContext().getRealPath(("/")));

So I figured out that the current working directory is:

C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27\bin

My question is that how can I set different directory address for each web application? I have many web applications and some of them use the same resource file names. I can't just put all of them in one directory.

Please note that I don't have access to the source code of jar files to change the. I just need a way to set the absolute path that the jar files use.

I have the same problem when I put the WAR file on the unix server. The extracted WAR file is in this location on the server:

/data02/tools/Apache/Tomcat/apache-tomcat-6.0.37/webapps/BANNEROnline

But I figure I should put the resource folders and files in this path (moosavi3 is my username!):

/home/moosavi3

How can I change the path?

Soheil
  • 5,229
  • 1
  • 18
  • 21
  • So do the jar files currently always try to load their resources from the current working directory? – Will Keeling Oct 22 '13 at 08:03
  • Yes. The jar files try to access to the files and folders in the current working directory. I don't know how to change it. – Soheil Oct 22 '13 at 19:29
  • What kind of jar files are we talking about? Homegrown? (why don't you have source for them?) Open Source? (same question) commercial? (who writes software with those impediments?) Could it be that you're missing a configuration option on the software? How do you pass the filenames into those applications? – Olaf Kock Oct 30 '13 at 10:42
  • I'm using Natural Language Processing libraries such as mallet.jar and Stanford-corenlp.jar. Some of the jar files need to load trained models, so they need to access to the file system. Some of them are not open source and the others are just ready to use. It's not reasonable to change the code and compile it again. And they have no external config file to specify the paths. – Soheil Oct 30 '13 at 14:31

2 Answers2

1

The working directory is the directory from which java.[exe,bin] is started. I assume the bin directory is where the tomcat start-up script is? If the jars are all using this working directory I don't believe there is a way to make different web-apps have different working directory, they're all loaded on the same jvm (java.exe) from the same working directory.

A working directory is the directory from which a binary is started, it is not some arbitrary value that you can change.

I suspect these jar files where meant to be run as standalone applications and expected the filesystem resources they are trying to access to be in the same location as themselves.

Any filesystem resources would have to be moved to the location of your java.exe so that the correct file path resolution can result from your jars.

Edmond
  • 21
  • 4
  • I know that regularly the tomcat looks into bin folder in the directory it's been installed for the resources. My question is that when I have my own code, I can say first program gets resources from "program1 resources", the second gets resources from relative path "program2 resources". Then I'll have these folders in the bin directory of tomcat and put the resources for each program in their own folder. So I can organize them. But the jar files I'm using in the projects, they need to access some resources and their relative path is set as ".", or bin folder of tomcat. How can I manage this? – Soheil Oct 30 '13 at 14:28
  • You can't do what you're asking...read my answer above, you can't dictate the working directory ('.')... if the jar files have hard-coded references to the working directory, then short of changing the jar code, there is no way to specify a different working directory. By definition a working directory is where a binary is started from, this goes for any kind of binary, not just java.exe The OS would always interpret '.' as meaning where the current binary process was started from. – Edmond Oct 31 '13 at 01:46
  • What you are saying must be true, based on my own experience. But I was hoping to find a way to specify the working directories for each application, because putting all the resources in the same directory doesn't seem well organized. – Soheil Oct 31 '13 at 17:19
0

Standard Servlet project requires external libraries to be placed in the 'WEB-INF/lib' directory under project root. You can search google for 'servlet directory structure' and do your own research for more information. Shared libraries between web applications can be placed in the 'lib' directory under tomcat root, they should be picked up by tomcat jvm. My recommendation would be to keep the dependencies project specific, because you may need different versions in different projects in the future.

Update: Read this page on the tomcat documentation, it will explain exactly how the project should be structured, and how to add a library that will be shared across all web applications:

http://tomcat.apache.org/tomcat-7.0-doc/appdev/deployment.html

Update 2: The following Stackoverflow link explains several options how to add a static file to your web application, that will available at runtime.

https://stackoverflow.com/a/2161583/940754

Update 3: Add a path to the classpath using the project's manifest:

http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Community
  • 1
  • 1
Rick Suggs
  • 1,582
  • 1
  • 15
  • 30
  • The documentation and the stackoverflow post didn't help :( I need a way to change the relative path which jar files start reading resources. And I don't have access to their code to use classloader or something like that. – Soheil Oct 30 '13 at 16:29
  • Are you telling me that you can't place the project specific files that the library imports into the project folder? The webapp specific classpath will automatically include WEB-INF/lib and WEB-INF/classes. The JVM automatically scans all entries in the classpath for resources, therefore any resource in the classpath will be found when 'jar files start reading resources'. – Rick Suggs Oct 30 '13 at 17:30
  • That's exactly true. I have placed the resources into WEB-INF/lib and WEB-INF/classes, but the program still cannot recognize it. The servlet that I wrote can access to these two folders and works fine, but the library jar file can't. – Soheil Oct 31 '13 at 17:15
  • And one question related to what you said: If the application has access to all the paths in classpath, how does it decide which one to use if we have the same folder or file name in two different paths which both are in class path? – Soheil Oct 31 '13 at 17:16
  • Did you place the resources directly into the WEB-INF/lib directory or is there a folder structure before you get to the actual files? What is this library you are using? Any documentation on what it expects? – Rick Suggs Oct 31 '13 at 17:51
  • Yes, they are directly in the WEB-INF/lib directory. – Soheil Nov 04 '13 at 16:40
  • In that case, we know that the libraries are using an absolute file path to get the files, now you should find out the required file path. It was a design mistake to make the library's configuration non-editable. You'll need to design your system around the mistake or use another library. – Rick Suggs Nov 04 '13 at 16:58
  • The libraries are not using the absolute paths for sure, because they can access to the data in the bin directory of Apache Tomcat or my default account path on Unix server. – Soheil Nov 06 '13 at 15:09
  • Excuse me, they must be using just the file name, reading it in with a FileInputStream (http://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html) which if given a filename, should only be able to read the file from the working directory. Again, design mistake. – Rick Suggs Nov 06 '13 at 20:22