Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: JAR'ing & Files

  1. #1
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default JAR'ing & Files

    Hey there, I've made a little program which reads program data from a .ser file. The program also gives the option to save back new data into the file.
    The problem is, when I JAR the program through eclipse, and run the JAR... it creates the .ser files outside of the JAR.

    Is there anyway I can have the file be stored inside the JAR so It gives the program better portability?
    I'm aware of accessing a file through getClass().getResourceAsStream(FILENAME), but I don't know how to therefore make it save the file inside the JAR, if that makes sense .

    Here's my read from file method:
    	@SuppressWarnings("unchecked")
    	public ArrayList<HoursOfTheWeek> readTableModel() {
    		ArrayList<HoursOfTheWeek> al = null;
    		try {
    			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILENAME));
    			al = (ArrayList<HoursOfTheWeek>) ois.readObject();
    		}
     
    		catch (Exception e) {
    			System.out.println("Problem reading back table from file: "
    					+ FILENAME);
     
    		}
     
    		if(al == null){
    			storeTableModel(defaultData());
    			readTableModel();
    		}
    		return al;
    	}


    And here's my store to file method:
    	public void storeTableModel(ArrayList<HoursOfTheWeek> al) {
    		ObjectOutputStream oos = null;
    		try {
    			oos = new ObjectOutputStream(new FileOutputStream(FILENAME));
    			oos.writeObject(al);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
     
    		try {
    			oos.close();
    		} catch (IOException e) {
    		}
    	}

    Thanks in advance.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JAR'ing & Files

    Is there anyway I can have the file be stored inside the JAR so It gives the program better portability?
    There was a thread on one of the forums about a project to update a jar file. Take a look at this thread:
    Help with manipulating files in a jar - Java
    I'm not sure you would be able to update the jar file you are executing from.

  3. The Following User Says Thank You to Norm For This Useful Post:

    newbie (August 18th, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JAR'ing & Files

    You can use a JarOutputStream to write a jar file.
    JarOutputStream (Java Platform SE 6)
    However I do not believe what you wish to do is possible.

  5. The Following User Says Thank You to copeg For This Useful Post:

    newbie (August 18th, 2011)

  6. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JAR'ing & Files

    Is there an alternative executable format which could let me do this then? Although it's not a critical issue, I don't like the feeling of needing to keep both the JAR and .ser file together where ever I move it to.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JAR'ing & Files

    An idea: copy contents of current jar to a new jar(if that is possible??) and add the .ser file to the new jar. Then the tricky bit. Start that new jar executing with a ProcessBuilder/Desktop or ?? and exit the current jar file's classes. When in the other jar file, delete the original jar file (pass commandline arg) and continue.

  8. The Following User Says Thank You to Norm For This Useful Post:

    newbie (August 18th, 2011)

  9. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JAR'ing & Files

    Sounds like quite the extra work for what I had expected to be a fairly simple ordeal
    Thanks for the suggestions, I'll look into them before deciding if I might be happy with leaving the file outside.
    Cheers!
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JAR'ing & Files

    Another problem would be the file names and locations. If in the same folder the new jar would need a new name.

  11. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JAR'ing & Files

    I'm not sure of the requirements you need. For instance, MS Word files don't travel with the Word application. Templates do, but reside in an accompanying directory. If you want your app to be portable with accompanying files (such as templates), deploy it with an installer that writes the appropriate files. If your app requires a file to be shared between instances, then create a central repository (server/database)

  12. #9
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: JAR'ing & Files

    How much data is it? Can you store it with java.util.prefs.Preferences? Is the requirement for portability because you want a lot of people to use your jar? If portability is important to you, then you probably shouldn't be using java.io.File anyway - I have my suspicions that some of us will be coding for platforms that don't have filesystems in the near future.

  13. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JAR'ing & Files

    I just made a timetable GUI so I could learn how to use a JTable.
    The Serialized ArrayList only holds about 5 objects, so It's nothing big.

    My program attempts to read the data from file, if a file doesn't exist, its automatically created and populated with default values.
    User also can change JTable data and save it back in.

    So the GUI will work without the file, but it will just have default values.
    I just wanted it so that the file couldn't be separated from the GUI so the user always has the data he/she put in.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  14. #11
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: JAR'ing & Files

    I've just added a little demo of a simple portable object store here:
    http://www.javaprogrammingforums.com...rsistence.html
    See what you think

Similar Threads

  1. How to create or edit .ser files in Jar files
    By xbill in forum Java IDEs
    Replies: 1
    Last Post: May 18th, 2011, 05:15 AM
  2. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:35 AM
  3. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM
  4. need help on jar files
    By kaka09 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 13th, 2010, 09:30 AM
  5. hlp files
    By vgenopoulos in forum Java Theory & Questions
    Replies: 1
    Last Post: July 9th, 2010, 09:11 AM