Results 1 to 8 of 8
- 11-09-2011, 08:01 PM #1
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.
- 11-09-2011, 08:46 PM #2
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?
- 11-09-2011, 08:58 PM #3
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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); } }
- 11-09-2011, 09:00 PM #4
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.
- 11-11-2011, 08:44 PM #5
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.
- 11-11-2011, 08:52 PM #6
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.
- 11-11-2011, 08:56 PM #7
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.
- 11-11-2011, 09:00 PM #8
Re: Read .txt from from .jar executable
There are several steps involved. You should read the Java tutorial for the details. Go to this site and Find Applet:How would I check/do giving the applet permission to read files.
The Really Big Index
There is a lot of doc there about using applets.
Similar Threads
-
create windows executable using executable jar file
By sarwar1234 in forum New To JavaReplies: 2Last Post: 02-07-2011, 08:29 PM -
Executable java to read and write to a txt file
By njoymirror in forum New To JavaReplies: 2Last Post: 03-21-2009, 01:19 AM -
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected
By kushagra in forum New To JavaReplies: 5Last Post: 10-17-2008, 02:13 PM -
executable
By smooth in forum New To JavaReplies: 4Last Post: 06-14-2008, 05:12 PM -
Executable JAR
By bugger in forum New To JavaReplies: 4Last Post: 12-05-2007, 05:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks