0

I am trying to write something to a ini file using ini4j.

When i call the store() method it throws a FileNotFound exception even though it is in my project directory.

Maybe i did something wrong with my code?

Main:

public class Main {

    public static Wini ini = null;

    public static void main(String[] args) {

        Config conf = new Config();
        try {
            conf.setMultiOption(true);
            ini = new Wini();
            ini.setConfig(conf);
            ini.load(new File("apikeys.ini"));
        } catch (InvalidFileFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

The file where i attempt to write and store the data:

if (KeyEndpoint.isValid(apikey)) {

                    Main.ini.put(apikey, Main.ini.get("Apikey"));

                    try {
                        Main.ini.store();
                    } catch (IOException e) {
                        channel.sendMessage("Invalid api key.").queue();
                    }

                } else {
                    channel.sendMessage("API Key is invalid.").queue();
                }

Any help is appreciated, I at least want to know what I am doing wrong.

Thanks!

1 Answer 1

0

It has to do with relative paths. Try changing the filename in Main for the absolute path, something like "/tmp/apikeys.ini", to check that your code works correctly. If that works, then you can now change it to either something like "../../directory/filename" or something relative to where you know you're executing your code from. Get familiar with your java path environment variables such as JAVA_HOME and all to get it to something more permanent and portable.

2
  • Still throwing the FileNotFound exception. only when I store it though, not when i load the config on main? Oct 26, 2020 at 19:41
  • can you post what you've run and the error you are getting?
    – palako
    Oct 26, 2020 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.