Results 1 to 3 of 3
  1. #1
    treckstar is offline Member
    Join Date
    Jan 2010
    Location
    Pennsylvania
    Posts
    3
    Rep Power
    0

    Default File Input - Signed JAR Applet

    Hello, I have been having quiet some trouble trying to solve my problem.
    I have a type of game applet that I have been developing.
    In the long run, the applet is going to be hosted on my server in an HTML file.

    The applet is compressed into a JAR along with all the files and images needed.
    I am trying to open a text file that is in the JAR, read it, and use the content accordingly.

    When I test the signed JAR applet locally on my machine, in an HTML file, it works flawlessly every time.
    When I upload the signed JAR to my server and then try to load it in an HTML file, it throws a "File Not Found" exception.
    All the other images load, they are also compressed in the JAR.
    I even tried placing copies of the file outside of the jar in the same directory, and still no luck.

    My objective is to be able to have the applet hosted on my web server, and it be able to find the file that is either compressed in the JAR, or located in the same directory as the applet.

    My code to open the "file".
    Java Code:
    File file = new File("level1.txt");
    Scanner scanner = new Scanner(file);
    Thanks!

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    getting the text file from within the .jar file is probably the simpler approach for an applet, otherwise you could use a URL and fetch it from a stored location on the same server where the applet was served from, and this probably would need a configuration setting in the applet init parameters, so all this is a lot of work to do if the file is read-only and doesn't ever change (the same for ever invocation of the applet and for every user who would use the applet). If the file needed to be different than the external file store would make more sense right.

    So , jar files are in the classpath, we can use a feature of the class loader to get a file, or a 'resource' located on the classpath as an input stream., the 'getResourceAsStream()'

    Java Code:
        InputStream in = LoadFromJarFile.class.getResourceAsStream("fileName.txt");
        if (in == null) {
          // unable to find the file in the class path (e.g. jar file)
          // do error here.
        }
        Scanner scanner = new Scanner(in);
    where the filename can be in a folder, e.g. package within the jar file (classpath). and we would denote this with "/" path delimiters.

  3. #3
    treckstar is offline Member
    Join Date
    Jan 2010
    Location
    Pennsylvania
    Posts
    3
    Rep Power
    0

    Default

    Thank you for the quick response.
    I actually just figured out how to read the text file through URL.
    I think I will give this try an approach

Similar Threads

  1. Signed Java Applet to read a file on hard drive?
    By ollyworks in forum Java Applets
    Replies: 2
    Last Post: 09-11-2009, 10:08 PM
  2. Replies: 1
    Last Post: 07-27-2009, 11:59 AM
  3. [SOLVED] signed applet, or what
    By AXH in forum Java Applets
    Replies: 2
    Last Post: 10-10-2008, 01:28 AM
  4. Replies: 2
    Last Post: 10-05-2008, 06:13 PM
  5. run java signed applet in vista
    By nanaji in forum Java Applets
    Replies: 7
    Last Post: 05-14-2008, 11:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •