Results 1 to 8 of 8
  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default A text file returns null in a jar file

    Hello. I am about to finish my first Java (and programming) project. After a few months of reading Java tutorials, I have completed the coding and can run it in Eclipse. I exported the project to a jar file and now got completely stuck.

    So, I have two text files located in a resource folder "lib" with airport latitude and longitude coordinates in the following format:

    AAA 10.00
    BBB 20.00
    CCC 30.00

    My Airports class gets the coordinates from the text file using HashMap:

    public class Airports {

    ... // other code
    public String getLng (String icaoCode) throws FileNotFoundException {

    Scanner in = new Scanner(new FileReader("lib/airportlng.txt"));
    HashMap<String, String> airports = new HashMap<String, String>();
    while (in.hasNextLine()) {
    String[] columns = in.nextLine().split(" ");
    airports.put(columns[0], columns[1]);
    }
    in.close();
    if(!airports.containsKey(icaoCode)) {
    message.portNotFoundMessage();
    }
    obtLng = airports.get(icaoCode);
    return obtLng;
    }

    ...

    }

    When I run a jar file, it returns 0. I have done some internet research and one suggestion is to use getResourceAsStream. So, should it be like the following?

    public String getLat (String icaoCode) throws FileNotFoundException {

    InputStream in = getClass().getResourceAsStream("lib/airportlat.txt");
    InputStreamReader isr = new InputStreamReader(in);
    BufferedReader br = new BufferedReader(isr);

    HashMap<String, String> airports = new HashMap<String, String>();

    while (in.hasNextLine()) { // HERE I HAVE AN ERROR AS hasNextLine METHOD IS UNDEFINED FOR INPUTSTREAM TYPE
    String[] columns = in.nextLine().split(" ");
    airports.put(columns[0], columns[1]);
    }

    in.close();

    if(!airports.containsKey(icaoCode)) {
    message.portNotFoundMessage();
    }

    obtLat = airports.get(icaoCode);

    return obtLat;

    }

    So, what would be the proper way to have my text file used with HashMap in a jar file? By the way, I have unzipped the jar file and the text files are there:

    com
    META-INF
    org
    airportlat.txt
    airportlng.txt

    Thanks a lot for your help!

    Best.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: A text file returns null in a jar file

    Hello and welcome! Please use [code][/code] tags when posting code so we can easily read it!

    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: A text file returns null in a jar file

    A jar file contains jar entries, not files. Check out the classes in the java.util.jar package.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: A text file returns null in a jar file

    Quote Originally Posted by igorland View Post
    Java Code:
    	public String getLat (String icaoCode) throws FileNotFoundException	{
    		
    		InputStream in = getClass().getResourceAsStream("lib/airportlat.txt");
    		InputStreamReader isr = new InputStreamReader(in);
    	              BufferedReader br = new BufferedReader(isr);
    		
    		HashMap<String, String> airports = new HashMap<String, String>();
    		
    		while (in.hasNextLine())	{                                                   // HERE I HAVE AN ERROR AS hasNextLine METHOD IS UNDEFINED FOR INPUTSTREAM TYPE
    			String[] columns = in.nextLine().split(" ");
    			airports.put(columns[0], columns[1]);
    		}
    		
    		in.close();
    		
    		if(!airports.containsKey(icaoCode))  {
    		    message.portNotFoundMessage();
    		}
    		
    		obtLat = airports.get(icaoCode);
    		
    		return obtLat;
    		
    	}
    So, what would be the proper way to have my text file used with HashMap in a jar file?
    The above is the correct way, however you have (properly) wrapped the input stream in a BufferedReader...except you don't end up using the BuffereReader.
    BufferedReader has a readLine() method. That will return null if you've reached the end of file.
    So use the reader and modify the code to use readLine() instead.

    The other option, if you are more comfortable with a Scanner, is to simply wrap the input stream in a Scanner.
    Please do not ask for code as refusal often offends.

  5. #5
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: A text file returns null in a jar file

    Thank you all, guys. Sorry about not using tags.

    So, I modified the code:

    Java Code:
    	public String getLat (String icaoCode) throws FileNotFoundException	{
    		
    		InputStream in = getClass().getResourceAsStream("lib/airportlat.txt");
    		InputStreamReader isr = new InputStreamReader(in);
    		BufferedReader br = new BufferedReader(isr);
    				
    		HashMap<String, String> airports = new HashMap<String, String>();
    		
    		try {
    			while (br.readLine() != null)	{
    				String[] columns = br.readLine().split(" ");
    				airports.put(columns[0], columns[1]);
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    				
    		if(!airports.containsKey(icaoCode))  {
    		    message.portNotFoundMessage();
    		}
    		
    		obtLat = airports.get(icaoCode);
    		
    		return obtLat;
    		
    	}
    Now, my file is not working at all throwing IOException. I wonder if I placed my text files correctly and correctly exported them to jar. I have attached snapshots of my file directory in Eclipse and the jar.

    Your response is much appreciated!

    A text file returns null in a jar file-jar1.jpgA text file returns null in a jar file-jar2.jpg
    Last edited by igorland; 01-22-2013 at 12:58 PM.

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

    Default Re: A text file returns null in a jar file

    The getResourceAsStream("lib/airportlat.txt"
    statement is looking on the classpath for the lib folder and in it for the file. Your display of the jar file's contents does not show a lib folder. Either include the lib folder in the jar file or remove the lib folder from the path in the getResourceAsStream call.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: A text file returns null in a jar file

    Quote Originally Posted by igorland View Post
    Java Code:
    while (br.readLine() != null)	{
    	String[] columns = br.readLine().split(" ");
    	airports.put(columns[0], columns[1]);
    }
    That will fail 50% of the time (ETA: and will only read every other line). You only want a single call to readLine as part of your loop.
    You would normally assign the result of that call to a String variable:
    Java Code:
    String line;
    while ((line = reader.readLine()) != null) {
        do something with 'line'.
    }
    Please do not ask for code as refusal often offends.

  8. #8
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: A text file returns null in a jar file

    Thank you so much! This code helped solve my problem:

    Java Code:
    	public String getLat (String icaoCode) throws FileNotFoundException	{
    		
    		@SuppressWarnings("resource")
    		Scanner in = new Scanner(this.getClass().getResourceAsStream("/airportlat.txt"));
    		
    		HashMap<String, String> airports = new HashMap<String, String>();
    		
    			while (in.hasNextLine())   {
    			String[] columns = in.nextLine().split(" ");
    			airports.put(columns[0], columns[1]);
    		}
    		
    
    		if(!airports.containsKey(icaoCode))  {
    		    message.portNotFoundMessage();
    		}
    		
    		obtLat = airports.get(icaoCode);
    		
    		return obtLat;
    		
    		}
    I love Java! Best!

Similar Threads

  1. Read from file returns zero, which is WRONG!
    By quinnvanorder in forum New To Java
    Replies: 5
    Last Post: 05-06-2012, 11:20 PM
  2. Replies: 2
    Last Post: 03-25-2011, 02:36 PM
  3. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  4. String file and carriage returns
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 01:28 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •