Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2008
    Posts
    48
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Netbeans executable jar with text file

    I'm trying to create an executable jar for the game I'm making. For ease of input and modification, I have a lot of the game data (monsters, skills, etc.) in text files. I created a package called resources with all the text files I need and put the package in the main package for the project. It works fine when I run it within netbeans, but it doesn't seem to be able to read from the text files when I run the .jar file that netbeans creates.

    The entire code of the game is a bit long to post everything, so I'll post snippets. The code for inputting monster data is:

    Code:
    public void importMonsters(){
    		Toolkit tk = Toolkit.getDefaultToolkit();
    		try{
    			BufferedReader count = new BufferedReader(new FileReader("resources/gamedata/monsters.txt"));
    			nMonsters = 0;
    			String countLine = count.readLine();
    			while(countLine != null){
    				nMonsters++;
    				countLine = count.readLine();
    			}
    			count.close();
    
    
    			BufferedReader minput = new BufferedReader(new FileReader("resources/gamedata/monsters.txt"));
    			minput.readLine();
    			String[] line;
    			for(int i = 1; i <= nMonsters; i++){
    				line = minput.readLine().split(":");
    
    
    				monster[i] = new Monster(
    						line[0],
    						Double.parseDouble(line[1]),
    						Double.parseDouble(line[2]),
    						Double.parseDouble(line[3]),
    						Double.parseDouble(line[4]),
    						Double.parseDouble(line[5]),
    						Integer.parseInt(line[6]),
    						Integer.parseInt(line[7]),
    						Integer.parseInt(line[8]),
    						Integer.parseInt(line[9])
    				);
    
    				monsterImage[i] = tk.getImage(getClass().getResource("resources/images/monsters/" + Integer.toString(i) +".gif"));
    				mt.addImage(monsterImage[i],0);
    			}
    		}catch(Exception e){}
    	}
    And the file, monsters.txt:

    Code:
    Name:atk:def:luk:agi:res:hp:type:exp:gold
    Dragon:5:3:6:5:5:30:1:5:7
    Flame Demon:6:5:2:2:9:25:1:8:10

    Finding help for this question has been difficult, since it's apparently rare for people to need to make executable .jar files with text files as input. If I really need to, I could get rid of the text files and enter the data directly into a .java file, but I'd much rather do it this way if possible.

    EDIT: I use images as well, and in order to make them work, I had to use getClass().getResource(filepath) instead of just using filepath. I've noticed from some source code that I found that people do something similar with text files in .jar files, but they're all using inputstream/scanner, which isn't something I'm use to using. Is there a way to do this with BufferedReader, or is there any simple way for me to read from these text files from a .jar?
    Last edited by blitz104; 01-30-2011 at 10:18 PM.

  • #2
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,982
    Thanks
    120
    Thanked 76 Times in 76 Posts
    For ease of input and modification

    As far as I know you cant modify anything inside resource files, so, drop the idea.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #3
    New Coder
    Join Date
    Jan 2011
    Location
    India
    Posts
    31
    Thanks
    0
    Thanked 4 Times in 4 Posts
    create the following folder resources/gamedata/
    under the present working directory and place your text files there... and then start your java program.

  • #4
    New Coder
    Join Date
    Jan 2011
    Location
    India
    Posts
    31
    Thanks
    0
    Thanked 4 Times in 4 Posts
    try this one also

    URL resource = Thread.currentThread().getContextClassLoader()
    .getResource(a/b/abc.txt);
    then get inputstream from url to load the file

  • #5
    New Coder
    Join Date
    Sep 2008
    Posts
    48
    Thanks
    7
    Thanked 0 Times in 0 Posts
    It won't be easily modifyable within the .jar, but while I'm developing it in netbeans, it's a lot more convenient to just have the text file open in a tab to modify quickly.

    I already have all of my text files in resources/gamedata/. It runs fine within netbeans, but it can't read the text files once it's been extracted to a .jar

    I wanted to avoid inputstream if possible. Is there a way to make it read line-by-line? My understanding is that it can only read character by character, which would be inefficient, and it would make it annoying for me to make it parse each variable in the lines.


  •  

    Posting Permissions

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