-1

Good morning, this is my method.

public void updatePropertiyByValue(String key, String value, String propFile) throws IOException {
            Properties prop = new Properties();
            objClassLoader = getClass().getClassLoader();
            InputStream objFileInputStream = null;
            objFileInputStream = getClass().getClassLoader().getResourceAsStream(propFile);
            prop.load(objFileInputStream);
            objFileInputStream.close();

            OutputStream output = null;
            try {
                output = new FileOutputStream(objClassLoader.getResource(propFile).getFile());
                prop.setProperty(key, value);
                prop.store(output, null);
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                output.close(); 
            }
        }

It's work is to update properties file. My problem is that the inputStream works good with the getResourceAsStream(propFile) instead the ouput part give me an error:

java.io.FileNotFoundException: file:*********************************\easyDeployment\EasyDeployment\target\EasyDeployment-0.0.1-SNAPSHOT.jar!\configuration.properties (The syntax of the file name, the directory name or the value label is not correct) // translated from my native language
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at Configuration.PropertiesMenager.updatePropertiyByValue(PropertiesMenager.java:58)
        at GraphicInterface.GInterface$3.actionPerformed(GInterface.java:328)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at Configuration.PropertiesMenager.updatePropertiyByValue(PropertiesMenager.java:64)
        at GraphicInterface.GInterface$3.actionPerformed(GInterface.java:328)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

I had this kind of error also when i used the getResource() method for the inputStream (in that case was precisely a fileInputStream). Is there a way to call the output in a different way?

  • Seems like you want to edit a file inside a jar-file. I think that is not possible (though I may be wrong) – Lino Feb 28 '19 at 09:40
  • Hi Lino, that's right…. It is a properties file inside my resources folder in my Jar. It works in "Run" mode of Eclipse, but it gives me this error when i call the produced Jar by command line. – Michael Mucci Feb 28 '19 at 09:46
  • When `getResource()` gives *what* error? – user207421 Feb 28 '19 at 10:25
  • It gives the exception 'FileNotFound' when I try to update the values inside the properties file... – Michael Mucci Feb 28 '19 at 10:41

1 Answers1

1

getResource() provides a URL to the file within the jar (zip file). Java can't open this directly.

You either need to expand the zip file to directory and update in the directory.

Or read in and change the zip file. I, however, must ask why you might do this? Class path resources are typically supposed to be read-only

  • It is a small application, without DB where i have to update some values! So as I can see it is not possible with .properties file… Only read is avalaible – Michael Mucci Feb 28 '19 at 09:58
  • Updating your own jar file is not the best. If this is on disk, can you write to a directory. In that case copy out the properties file to a file on disk, then you can update it as you like. – Daniel Sagenschneider Feb 28 '19 at 10:01
  • That could be an idea… there is an easy way to update the jar file with the "directory file updated" at the end of the session? – Michael Mucci Feb 28 '19 at 10:11
  • https://stackoverflow.com/questions/11502260/modifying-a-text-file-in-a-zip-archive-in-java – Daniel Sagenschneider Feb 28 '19 at 10:18