javaのswingでアプリケーションを作っています。
【Java】設定ファイル(propertiesやini)の値をキー指定で書き換える
↑こちらのサンプルを参考にpropertiesファイルの読み書きを行うプログラムを作ったのですが、
eclipseから実行可能jarでエクスポートしたところ、書き込みができなくなりました。
読み込みはできるのでファイルのURLは問題ないと思われます。
jarを解凍して中を確認しましたが、classファイルと同じ階層にpropertiesファイルが
入っていることも確認しました。
質問用に問題の部分を抽出したコードを載せます。
java
1import java.awt.BorderLayout; 2import java.net.URL; 3import javax.swing.JFrame; 4import javax.swing.JLabel; 5import org.apache.commons.configuration2.PropertiesConfiguration; 6import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; 7import org.apache.commons.configuration2.builder.fluent.Configurations; 8 9public class SaveProperties extends JFrame { 10 JLabel label; 11 URL url = getClass().getClassLoader().getResource("conf.properties"); 12 13 public static void main(String args[]){ 14 SaveProperties frame = new SaveProperties("SaveProperties"); 15 frame.setVisible(true); 16 } 17 18 SaveProperties(String title){ 19 setTitle(title); 20 setSize(200, 120); 21 setLocationRelativeTo(null); 22 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 23 setLayout(new BorderLayout()); 24 25 label = new JLabel(); 26 try { 27 save(); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 32 add(label); 33 } 34 35 public void save() throws Exception { 36 Configurations configs = new Configurations(); 37 FileBasedConfigurationBuilder<PropertiesConfiguration> builder = 38 configs.propertiesBuilder(url); 39 PropertiesConfiguration config = builder.getConfiguration(); 40 config.setProperty("id", "1234"); 41 builder.save(); 42 label.setText("ok"); 43 } 44}
//conf.propertiesファイルの中身 id = 0000
eclipseで実行したときは理想通りclassファイルと同じ階層にあるpropertiesファイルの中身が
id = 1234
になってくれるのですが、実行可能jarから実行すると0000のままです。
ちなみにこの質問用のコードではokと表示されるのですが、実際のコードでは
builder.save();の前にlabel.setText("ok");を置いたときしか表示されませんでした。
原因や解決策などわかる方がいましたら、助けていただけると幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/01 13:22