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.
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);
}
}
Re: Dynamic loading property File from JAR file
Quote:
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.
Quote:
THIS ISN'T WORK GOOD
Can you explain your problem with this program?
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.)
Re: Dynamic loading property File from JAR file
Files stored in jars are read only.
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.
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)
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.