Results 1 to 8 of 8
  1. #1
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default Read .txt from from .jar executable

    I am trying to read 6 .txt files into a .jar executable file. When I click on buttons and such on my JApplet, no text comes up as it does when I run in netbeans. It is a JApplet thats on a Frame that was created through netbeans GUI builder. The .jar was also created though netbeans. So how can I read files into my .jar and do they need to be in the same directory or can the .jar store them internally?

    I read this Java Jar file - how to read a file from a Jar file | Java jar file reader | devdaily.com but it does not make a lot of sense to me. I tried using some of that code with no success.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Read .txt from from .jar executable

    Are the .txt files you are trying to read in the jar file or are they outside in a folder on the PC?

    What code are you using to read the files?

  3. #3
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default Re: Read .txt from from .jar executable

    The files are in the same package as the rest of the project in netbeans. The jar that is created is in a different folder, the dist folder. Im not sure if netbean adds the .txt files to the .jar.

    Java Code:
    public void fillList () throws FileNotFoundException
        {
            classList = new Class[DEFAULT_SIZE];
            size = 0;
            Scanner classFile = new Scanner(new File (classFileName));
            Scanner infoFile = new Scanner(new File (infoFileName));
            this.name = classFile.next();
            while(classFile.hasNextLine())
            {
                Class temp = new Class(classFile.nextLine(), infoFile.nextLine());
                this.add(temp);
            }
        }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Read .txt from from .jar executable

    Are you sure the code is looking in the correct folder for the file?
    Create an instance of the File class for the file and Print out the File class's absolutePath to see where the program is looking for the file.
    Last edited by Norm; 11-09-2011 at 09:07 PM.

  5. #5
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default Re: Read .txt from from .jar executable

    The absolute file path is correct, it was "C:\Users\...\ClassNames.txt" and such for all 6 files. Would I need to include some statements in my .java file to tell the jar executable to place these files inside the .jar? I did not include any such statements, besides what is needed inside netbeans "run project" to make the program execute properly. Is it possible to have the .txt files inside .jar or will they always needs to be read externally?
    Last edited by patriotsfan; 11-11-2011 at 08:52 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Read .txt from from .jar executable

    If the path is correct, does the code read the files successfully?
    If this is an applet, does the applet have permission to read the files?
    Are there any error messages in the java console?

    I have no idea how to make your IDE put files in your jar file.

    If these files are the ones your applet uses, then you should put them into the jar file and treat them as resources instead of as files.
    You could use the getResourceAsStream() method to get an InputStream and then read the files from that.

  7. #7
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default Re: Read .txt from from .jar executable

    When inside netbeans and I use "run project" the .txt files are read properly and display text when needed but not when I create the .jar. No errors present inside console. How would I check/do giving the applet permission to read files. I will try using the "getResourceAsStream() method to get an InputStream" as you suggested.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Read .txt from from .jar executable

    How would I check/do giving the applet permission to read files.
    There are several steps involved. You should read the Java tutorial for the details. Go to this site and Find Applet:
    The Really Big Index

    There is a lot of doc there about using applets.

Similar Threads

  1. create windows executable using executable jar file
    By sarwar1234 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 08:29 PM
  2. Executable java to read and write to a txt file
    By njoymirror in forum New To Java
    Replies: 2
    Last Post: 03-21-2009, 01:19 AM
  3. Replies: 5
    Last Post: 10-17-2008, 02:13 PM
  4. executable
    By smooth in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 05:12 PM
  5. Executable JAR
    By bugger in forum New To Java
    Replies: 4
    Last Post: 12-05-2007, 05:41 PM

Posting Permissions

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