This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I have an applet that needs to reads from a text file. My project structure is as follows:
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.
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.
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!