input file not found

Hi there,

I'm trying to read in an input file with a jsp file. The reading itself is done by a class that is imported by the jsp file.

Now this all works fine, but the problem is that he can't find the specific input file

Does anyone now where I have to place the input file on the server (using tomcat5.5) .

I tried to place it in the same directory where I strored the .class file that wants to read it. Unfortunenately it it's not working

I hope someone really can help me out.

thank you in advance

[551 byte] By [mol-mola] at [2007-10-3 0:54:24]
# 1
You either have to specify the full path to the file in your JSP, or use YourClass.class.getResource() to get the resource from the classpath.
Herko_ter_Horsta at 2007-7-14 17:49:44 > top of Java-index,Java Essentials,New To Java...
# 2

You mean the classpath on the server? About trhe getResource, he can't find the file, so I can't call this function. I need to know where I should store a input file so my class file can read it (using java I/I) I know where is shoud be stored when it concerns a java application, but with the webapplication it is apperently different.

Message was edited by:

mol-mol

Message was edited by:

mol-mol

mol-mola at 2007-7-14 17:49:44 > top of Java-index,Java Essentials,New To Java...
# 3

Like I said, you can store it anywhere you want as long as you know the absolute path to the file on the server filesystem.

Assuming this isn't an option, you will have to get it from the classpath using getResource(). The classpath of a webapplication includes all jar files in WEB-INF/lib as well as WEB-INF/classes

So you can either put the file in a jar along with the classes (stored in the WEB-INF/lib directory) or put it in WEB-INF/classes.

Herko_ter_Horsta at 2007-7-14 17:49:44 > top of Java-index,Java Essentials,New To Java...
# 4
> About trhe getResource, he can't find the file, so I can't call> this function.Yes you can, and should. If you haven't got that working yet, that doesn't mean it's the wrong method (function) to call, it's just that you're not doing it right yet.
warnerjaa at 2007-7-14 17:49:44 > top of Java-index,Java Essentials,New To Java...
# 5

I want to read data from an Excel file. I can do this with java by using POI. The point is that this file resides on the tomcat server and that I use a java file to read from it. It works when I use the full path that I get from your proposed function. Is there a nicer way to read data from a file that resides on the same server as the application does.?

Maybe i can do something with JSP itself....

You see it would be nice if I could use a relatative path instead of an absolute one.

Message was edited by:

mol-mol

mol-mola at 2007-7-14 17:49:44 > top of Java-index,Java Essentials,New To Java...
# 6

You can't and you shouldn't use a relative path anyway because it is too fragile.

Relative paths are resolved relative to the "current directory" at startup. Since you can start a Java application from any location assuming you set the classpath right, your program will stop working if you don't start it from the same directory that contains your classes.

Obviously, in the case of Tomcat, Java will be started from a different location. I'm pretty sure you can pass an InputStream to POI, it doesn't have to be a file. YourClass.class.getResourceAsStream() will get you an InputStream on a resource on your classpath. The path you have to pass to that function is relative to the classpath of the web application. This will work if you package the Excel file with your application (putting it in a jar with your classes or in WEB-INF/classes), which is what I understood you wanted.

Herko_ter_Horsta at 2007-7-14 17:49:44 > top of Java-index,Java Essentials,New To Java...
# 7

Hi,

I tried to use the absolute path I obtained from the Yourclass.class.getResource() function. It works locally (the path is something like D:\test\test.xls) but on the server (which is a Linux Redhead) It is not working. The path I obtain is like /usr/local/........../classes/myClass.class. When I use this path with istead of myClass.class, test.xls at the end. It is still not working, though it works on my own PC.

Do you have any idea?

thnx for helping me by the way

mol-mola at 2007-7-14 17:49:44 > top of Java-index,Java Essentials,New To Java...
# 8
have u try application.getRealPath("file.extension"); ?
Jefry_Xua at 2007-7-14 17:49:44 > top of Java-index,Java Essentials,New To Java...
# 9
It's finally working. No I didn't, but application.getRealPath(myClass.class) gives the same result as Yourclass.getResource(Yourclass.class);thnx all
mol-mola at 2007-7-14 17:49:44 > top of Java-index,Java Essentials,New To Java...