Results 1 to 7 of 7
  1. #1
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    16
    Rep Power
    0

    Default Dynamic loading property File from JAR file

    Hello, I would ask, whether it's possible possible to have a file in JAR of program, because when i create executable JAR from my example, so I have property file(config.properties) in the same folder as JAR, but I need property file in JAR file and then I need read from this property file. I don't know, whether my way is good solution. I have everything in one class, this is me just about functionality, then it will use in the project. Thanks.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Main {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            String userDir = System.getProperty("user.dir");
            System.out.println(userDir);
            
            gui();
        }
    
        private static void gui() {
            JFrame f = new JFrame("Passive Text Field");
            f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
            final JTextField tf1 = new JTextField(32);
            final JTextField tf2 = new JTextField(32);
            JPanel p = new JPanel();
            JButton write = new JButton("INSERT");
            JButton read = new JButton("READ");
            p.add(write);
            p.add(read);
            f.getContentPane().add(tf1);
            f.getContentPane().add(tf2);
            f.getContentPane().add(p);
    
            ActionListener l = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("Action event from a text field");
                }
            };
            tf1.addActionListener(l);
    
            // Make the button the default button
            f.getRootPane().setDefaultButton(write);
            write.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("Content of text field: <" + tf1.getText()
                            + ">");
                    writeProperty();
                }
    
                private void writeProperty() {
                    Properties prop = new Properties();
                    try {
                        // set the properties value
                        prop.setProperty(tf1.getText(), tf2.getText());
                        // save properties to project root folder
                        prop.store(new FileOutputStream("config.properties"), null);
                    }
                    catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
    
            read.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("READING");
                    readProperty();
                }
    
                private void readProperty() {
                    Properties prop = new Properties();
                    
                    try {
                           //load a properties file
                        prop.load(new FileInputStream(System.getProperty("user.dir")+"\\config.properties"));
    //   THIS ISN'T WORK GOOD :-( -->  prop.load(this.getClass().getClassLoader().getResourceAsStream("config.properties"));
                        
                        System.out.println(prop.getProperty(tf1.getText()));
                        JOptionPane.showMessageDialog(null, prop.getProperty(tf1.getText()));
             
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
    
            f.pack();
            f.setVisible(true);
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Dynamic loading property File from JAR file

    possible to have a file in JAR of program
    Yes it is possible to have any file in a jar file. The problem could be how you read the file. You can treat the file as a resource and use the Class class's getResource methods to read it.
    You can't write to the file while it is in the jar file that you are executing classes from.

    THIS ISN'T WORK GOOD
    Can you explain your problem with this program?

  3. #3
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic loading property File from JAR file

    I have problem with saving file in JAR package. I don't know how create and save in JAR. Config.properties file I have in Project Folder, but in jat this file missing. Property file will have some values, which I can change only sometimes(if it's possible). And after running my program above I have property file in same folder as JAR file. Can you help me with this?

    And when I need to change values in property file, so I have to to have file out of JAR file?? (I want to one time run program and I writes a value to the properties file. And when I run JAR second time, three time, .... so I need last values. It is trying to do with my program above.)

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

    Default Re: Dynamic loading property File from JAR file

    Files stored in jars are read only.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Dynamic loading property File from JAR file

    You can't update a file in a jar file while executing classes from the jar file.

  6. #6
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic loading property File from JAR file

    Forsake ... and how create xxx.properties, when I want xxx.properties in JAR. I have xxx.properties in root folder on project, but after build I don't see xxx.properties in file or I can't read values? (I use eclipse)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Dynamic loading property File from JAR file

    If you have a question about how to use your IDE, ask it on the section of the forum for IDEs.
    Eclipse

    I use the jar command to create my jar files.

Similar Threads

  1. Property file can't be read
    By Tulsa in forum New To Java
    Replies: 2
    Last Post: 02-11-2011, 05:49 PM
  2. Property file cannot be read
    By Tulsa in forum JDBC
    Replies: 3
    Last Post: 02-11-2011, 04:48 PM
  3. File Property
    By Juggler in forum New To Java
    Replies: 0
    Last Post: 08-10-2008, 07:23 AM
  4. Initializing property object from a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:55 PM
  5. Property File
    By Peter in forum JDBC
    Replies: 2
    Last Post: 07-04-2007, 03:48 PM

Posting Permissions

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