Results 1 to 9 of 9
  1. #1
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Default setComment not working for me/how to write to a text file in a jar

    In a project I have been working on, I wanted to reference text files by writing to and reading from them, but I have learned that it is very difficult to do that when they are in a JAR file. Instead, I am hoping to use the comment field. Here is the code that I have:

    Java Code:
    public class run
    {
    public static File sound = new File("sound.txt");
    public static File minutes = new File("minutes.txt");
    public static File Shutdown = new File("Shutdown.txt");
    public static File killInternet = new File("killInternet.txt");
    public static File seconds = new File("seconds.txt");
    
    public static void main(String[] args)
    {
    setZip(sound,"1");
    setZip(minutes,"1");
    setZip(Shutdown,"1");
    setZip(killInternet,"1");
    setZip(seconds,"1");
    
    JOptionPane.showMessageDialog(null,getZip(sound));
    JOptionPane.showMessageDialog(null,getZip(minutes));
    JOptionPane.showMessageDialog(null,getZip(Shutdown));
    JOptionPane.showMessageDialog(null,getZip(killInternet));
    JOptionPane.showMessageDialog(null,getZip(seconds));
    
    
    }
           public static void setZip(
           	 File file, String tester) {
    
     try{
         System.out.println(file.getName());
          JarFile jarFile = new JarFile("Timer.jar");
    
          JarEntry entry = jarFile.getJarEntry(file.getName());
          entry.setComment(tester);
    
    
    }
    catch(Exception nevergonnahappen)
    {
    
    JOptionPane.showMessageDialog(null, nevergonnahappen.getMessage());
    
    }
        }
               public static String getZip(
           	 File file) {
    
     try{
         System.out.println(file.getName());
          JarFile jarFile = new JarFile("Timer.jar");
    
          JarEntry entry = jarFile.getJarEntry(file.getName());
          String end = entry.getComment();
          return end;
    
    
    }
    catch(Exception nevergonnahappen)
    {
    
    JOptionPane.showMessageDialog(null, nevergonnahappen.getMessage());
    return "rerER";
    }
        }
    }
    I make it into a jar, and I get blank messages every time, and the comment fields are not being changed. Any help would be appreciated.
    Last edited by Eranga; 08-05-2010 at 04:53 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There are no "files" inside of a Jar. This is a bad idea and I suggest that you don't do this. Instead, why not pass the path to the non-jarred text file into your program via command line arguments.

  3. #3
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Default

    yes, I realize that there are not, but for these purposes, it needs to be all contained inside of a jar file. I need to save user data as defaults, so I am using text files(or at least the comments now).

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by minime12358 View Post
    yes, I realize that there are not, but for these purposes, it needs to be all contained inside of a jar file.
    Are you absolutely sure? Wouldn't you be better off to change these requirements to be reasonable for the tools that you are using?

    I need to save user data as defaults, so I am using text files(or at least the comments now).
    Have you looked into using Properties to hold your configuration information? That's what they're for.

  5. #5
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Default

    that will help, but what is wrong with my code now?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Archive files (of which jars are a part) are not really intended to have the contents of individual "files" changed like that. You really ought to use a separet properties file in a location relative to the jar file. Something like:
    Java Code:
    app_folder
        - myapp.jar
        - lib (contains any dependent jars, but it sounds like you don't have any)
        - config
            - myapp.properties
    Essentially like any other application. In fact, if this is deployed as a general app on a machine so it can be used by more than one user account you can hold the config stuff under the user.home (System.getProperty("user.home")), something like:
    Java Code:
    .myapp
        - config
            - myapp.properties
    ETS: Going with the second one (which might be more suitable for you), if that directory or properties file does not exist then you simply default to the base one in the jar file.

  7. #7
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Default

    I do realize that that is the most logical thing, but how would I change the comments on a JarEntry? this clearly is not working....

  8. #8
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Default

    or another question... what is the setComment("") used for if it cannot be used to change the comment in a zip

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    I have no idea, because I've never used it.
    However I would bet that it was not intended to be used as a substitue for a properties file.

Similar Threads

  1. Replies: 10
    Last Post: 05-24-2010, 08:25 PM
  2. How to write text file into Array
    By venkat.ravala in forum New To Java
    Replies: 13
    Last Post: 11-19-2009, 04:59 PM
  3. How to write a string middle of a text file?
    By loggen in forum New To Java
    Replies: 5
    Last Post: 12-19-2008, 08:48 AM
  4. Write a List into a Text file
    By tech2000 in forum New To Java
    Replies: 1
    Last Post: 11-13-2008, 05:09 AM
  5. how do i write to a text file from an arraylist?
    By otoro_java in forum New To Java
    Replies: 3
    Last Post: 01-30-2008, 06:53 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
  •