File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Proper location of external file for applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Proper location of external file for applet" Watch "Proper location of external file for applet" New topic
Author

Proper location of external file for applet

Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

I have an applet that needs to reads from a text file. My project structure is as follows:

--dir src
   --dir appletPackage
       *.java files

--dir bin
   --dir classes
    appletText.txt
    --dir appletPackage
        *.class files

In the function that starts to read from the text file, I simply use a relative path.



When I run the application in Eclipse, it runs fine. However, when I run the application as an application from a web page, the application cannot find the text file. My JAR is packaged just like the Eclipse directory structure. My applet tag is as follows:



My applet runs fine, but when I access the function that references the appletText.txt file, I get the FileNotFound Exception. I tried changing the "code" attribute to read , but that didn't work either.

Where does the file need to be?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Well, first of all, you aren't running it as an application from a web page, are you? Your HTML clearly indicates that it's an applet. Not an application, which runs by itself without any help from a browser.

Anyway if you want to use that code, then the file has to be in the browser's default directory. Two problems with that: (1) you don't know what it is, and (2) it is on the client, not on the server.

I assume you want the file to remain on the server, and your applet should retrieve it from there. In which case you shouldn't use a File object to access it, since File objects refer to files on the machine where the code is running.

One option is to put it into the Applet.jar file, then access it like this:

That's assuming it's in the root of the jar. If it's in some subdirectory, then specify that as part of the path.
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Thanks for your response Paul.

I will try the direct approach you suggested, but I think I am doing something similar already. I apologize for not supplying more information earlier, but I didn't realize the difference between the files as you defined.

Here is the full function:


I am using an InputStream, but I don't call the getClass().getResource() method like you suggest. I'll give your suggestion a try, and let you know what happens!
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Paul Clapham wrote:One option is to put it into the Applet.jar file, then access it like this:

That's assuming it's in the root of the jar. If it's in some subdirectory, then specify that as part of the path.


Thank you Paul, it totally worked. I still had to keep a copy of the file in bin/classes, in order to run the program in Eclipse.

Thank you soo much!

Amanda
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Proper location of external file for applet
 
Similar Threads
Has anyone seen this error before??
Ant javac task and subdirectories
Access & Read Embedded Email Attachments From Existing Email Message
Select & Convert Memory Stream Image to PDF File Using Aspose.Pdf
Need Help With Dynamic text box in JSP