jar file built with maven can't find configuration file












-1















I built a jar file with maven. I am using the following code in the only class in the jar file to try to access the config file.



 InputStream is = Myclass.class.getClassLoader().getResourceAsStream("/config.properties");


The config.properties file is in the jar file located at Path /



I don't see any error messages. But when I try to access the values in the Properties object, the values are null.



 Properties prop = new Properties();
prop.load(is);
serverUrl=prop.getProperty("serverUrl");


serverUrl is null.



Any suggestions on how to get the values in the config file in the jar file?










share|improve this question

























  • you should add the error that you are getting

    – Nawnit Sen
    Nov 16 '18 at 7:43











  • Try Myclass.class.getClassLoader().getResourceAsStream("config.properties") or put your config in same package as MyClass.

    – talex
    Nov 16 '18 at 7:51











  • no error message that I can see, but when I try to access the values, there is a null value or a null pointer exception

    – user840930
    Nov 16 '18 at 8:37
















-1















I built a jar file with maven. I am using the following code in the only class in the jar file to try to access the config file.



 InputStream is = Myclass.class.getClassLoader().getResourceAsStream("/config.properties");


The config.properties file is in the jar file located at Path /



I don't see any error messages. But when I try to access the values in the Properties object, the values are null.



 Properties prop = new Properties();
prop.load(is);
serverUrl=prop.getProperty("serverUrl");


serverUrl is null.



Any suggestions on how to get the values in the config file in the jar file?










share|improve this question

























  • you should add the error that you are getting

    – Nawnit Sen
    Nov 16 '18 at 7:43











  • Try Myclass.class.getClassLoader().getResourceAsStream("config.properties") or put your config in same package as MyClass.

    – talex
    Nov 16 '18 at 7:51











  • no error message that I can see, but when I try to access the values, there is a null value or a null pointer exception

    – user840930
    Nov 16 '18 at 8:37














-1












-1








-1








I built a jar file with maven. I am using the following code in the only class in the jar file to try to access the config file.



 InputStream is = Myclass.class.getClassLoader().getResourceAsStream("/config.properties");


The config.properties file is in the jar file located at Path /



I don't see any error messages. But when I try to access the values in the Properties object, the values are null.



 Properties prop = new Properties();
prop.load(is);
serverUrl=prop.getProperty("serverUrl");


serverUrl is null.



Any suggestions on how to get the values in the config file in the jar file?










share|improve this question
















I built a jar file with maven. I am using the following code in the only class in the jar file to try to access the config file.



 InputStream is = Myclass.class.getClassLoader().getResourceAsStream("/config.properties");


The config.properties file is in the jar file located at Path /



I don't see any error messages. But when I try to access the values in the Properties object, the values are null.



 Properties prop = new Properties();
prop.load(is);
serverUrl=prop.getProperty("serverUrl");


serverUrl is null.



Any suggestions on how to get the values in the config file in the jar file?







java maven jar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 9:33







user840930

















asked Nov 16 '18 at 7:39









user840930user840930

1,438144263




1,438144263













  • you should add the error that you are getting

    – Nawnit Sen
    Nov 16 '18 at 7:43











  • Try Myclass.class.getClassLoader().getResourceAsStream("config.properties") or put your config in same package as MyClass.

    – talex
    Nov 16 '18 at 7:51











  • no error message that I can see, but when I try to access the values, there is a null value or a null pointer exception

    – user840930
    Nov 16 '18 at 8:37



















  • you should add the error that you are getting

    – Nawnit Sen
    Nov 16 '18 at 7:43











  • Try Myclass.class.getClassLoader().getResourceAsStream("config.properties") or put your config in same package as MyClass.

    – talex
    Nov 16 '18 at 7:51











  • no error message that I can see, but when I try to access the values, there is a null value or a null pointer exception

    – user840930
    Nov 16 '18 at 8:37

















you should add the error that you are getting

– Nawnit Sen
Nov 16 '18 at 7:43





you should add the error that you are getting

– Nawnit Sen
Nov 16 '18 at 7:43













Try Myclass.class.getClassLoader().getResourceAsStream("config.properties") or put your config in same package as MyClass.

– talex
Nov 16 '18 at 7:51





Try Myclass.class.getClassLoader().getResourceAsStream("config.properties") or put your config in same package as MyClass.

– talex
Nov 16 '18 at 7:51













no error message that I can see, but when I try to access the values, there is a null value or a null pointer exception

– user840930
Nov 16 '18 at 8:37





no error message that I can see, but when I try to access the values, there is a null value or a null pointer exception

– user840930
Nov 16 '18 at 8:37












3 Answers
3






active

oldest

votes


















0














Typically in maven, you locate resources under src/main/resources.



Then you can access these resources by using the classloader:



try (InputStream in = Myclass.class.getClassLoader().getResourceAsStream("config.properties") {
//doSomething
}





share|improve this answer
























  • Thank you for your comment! I updated my question and code to reflect the change. So my code appears to access the file but the values are null.

    – user840930
    Nov 16 '18 at 9:09











  • Or at least I should say InputStream is no longer null

    – user840930
    Nov 16 '18 at 11:59



















0














I was facing same issue before I realized that the following way is only good for loading files in your classpath.



InputStream is = Myclass.class.getClassLoader().getResourceAsStream("/config.properties");


For picking files regardless of whether they are in classpath or not, I use Apache FileUtils. For e.g I used following and it worked like a charm.



String str = FileUtils.readFileToString(new File(path_to_your_file), "UTF-8");
IOUtils.toInputStream(str, "UTF-8"); // if you need InputStream object


You can have a look at other APIs provided by FileUtils too other than readFileToString






share|improve this answer































    0














    getResourceAsStream("config.properties") - this method will only work if your properties file is in the classpath. I think you have added the properties file outside the classpath. This method is basically used for accessing a file present in the classpath.



    Please check it again.



    Thanks :)



    Working Code :



    public class MyClass {

    public static void main(String args) throws Exception {
    Properties prop = new Properties();
    InputStream input = null;

    try {

    String filename = "config.properties";
    input = MyClass.class.getClassLoader().getResourceAsStream(filename);
    if (input == null) {
    System.out.println("Sorry, unable to find " + filename);
    return;
    }

    // load a properties file from class path, inside static method
    prop.load(input);

    // get the property value and print it out
    System.out.println(prop.getProperty("serverUrl"));

    } catch (IOException ex) {
    ex.printStackTrace();
    } finally {
    if (input != null) {
    try {
    input.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    }

    }





    share|improve this answer


























    • how do I add the file to the classpath?

      – user840930
      Nov 16 '18 at 8:29






    • 1





      I'm pretty sure the file is being found now. I changed the code to getResourceAsStream("/config.properties");

      – user840930
      Nov 16 '18 at 10:00













    • keep the properties file in the resources folder of your project.. Then if you are using eclipse. Then right click on the project -> Properties ->Java Build Path -> click on source tab -> Click on Add folder and add the resources folder -> Click on Apply -> Click on Apply and close. Make a little modification to getResourceAsStream("resources/config.properties")

      – Anish B.
      Nov 16 '18 at 10:04













    • tried "resources/config.properties. That did not work. Can't access the file. NullPointerException. The correct way looks like "/config.properties"

      – user840930
      Nov 16 '18 at 11:06











    • then its fine :)

      – Anish B.
      Nov 16 '18 at 12:39











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333386%2fjar-file-built-with-maven-cant-find-configuration-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Typically in maven, you locate resources under src/main/resources.



    Then you can access these resources by using the classloader:



    try (InputStream in = Myclass.class.getClassLoader().getResourceAsStream("config.properties") {
    //doSomething
    }





    share|improve this answer
























    • Thank you for your comment! I updated my question and code to reflect the change. So my code appears to access the file but the values are null.

      – user840930
      Nov 16 '18 at 9:09











    • Or at least I should say InputStream is no longer null

      – user840930
      Nov 16 '18 at 11:59
















    0














    Typically in maven, you locate resources under src/main/resources.



    Then you can access these resources by using the classloader:



    try (InputStream in = Myclass.class.getClassLoader().getResourceAsStream("config.properties") {
    //doSomething
    }





    share|improve this answer
























    • Thank you for your comment! I updated my question and code to reflect the change. So my code appears to access the file but the values are null.

      – user840930
      Nov 16 '18 at 9:09











    • Or at least I should say InputStream is no longer null

      – user840930
      Nov 16 '18 at 11:59














    0












    0








    0







    Typically in maven, you locate resources under src/main/resources.



    Then you can access these resources by using the classloader:



    try (InputStream in = Myclass.class.getClassLoader().getResourceAsStream("config.properties") {
    //doSomething
    }





    share|improve this answer













    Typically in maven, you locate resources under src/main/resources.



    Then you can access these resources by using the classloader:



    try (InputStream in = Myclass.class.getClassLoader().getResourceAsStream("config.properties") {
    //doSomething
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 7:57









    Tobias Bertram-KöhlerTobias Bertram-Köhler

    334




    334













    • Thank you for your comment! I updated my question and code to reflect the change. So my code appears to access the file but the values are null.

      – user840930
      Nov 16 '18 at 9:09











    • Or at least I should say InputStream is no longer null

      – user840930
      Nov 16 '18 at 11:59



















    • Thank you for your comment! I updated my question and code to reflect the change. So my code appears to access the file but the values are null.

      – user840930
      Nov 16 '18 at 9:09











    • Or at least I should say InputStream is no longer null

      – user840930
      Nov 16 '18 at 11:59

















    Thank you for your comment! I updated my question and code to reflect the change. So my code appears to access the file but the values are null.

    – user840930
    Nov 16 '18 at 9:09





    Thank you for your comment! I updated my question and code to reflect the change. So my code appears to access the file but the values are null.

    – user840930
    Nov 16 '18 at 9:09













    Or at least I should say InputStream is no longer null

    – user840930
    Nov 16 '18 at 11:59





    Or at least I should say InputStream is no longer null

    – user840930
    Nov 16 '18 at 11:59













    0














    I was facing same issue before I realized that the following way is only good for loading files in your classpath.



    InputStream is = Myclass.class.getClassLoader().getResourceAsStream("/config.properties");


    For picking files regardless of whether they are in classpath or not, I use Apache FileUtils. For e.g I used following and it worked like a charm.



    String str = FileUtils.readFileToString(new File(path_to_your_file), "UTF-8");
    IOUtils.toInputStream(str, "UTF-8"); // if you need InputStream object


    You can have a look at other APIs provided by FileUtils too other than readFileToString






    share|improve this answer




























      0














      I was facing same issue before I realized that the following way is only good for loading files in your classpath.



      InputStream is = Myclass.class.getClassLoader().getResourceAsStream("/config.properties");


      For picking files regardless of whether they are in classpath or not, I use Apache FileUtils. For e.g I used following and it worked like a charm.



      String str = FileUtils.readFileToString(new File(path_to_your_file), "UTF-8");
      IOUtils.toInputStream(str, "UTF-8"); // if you need InputStream object


      You can have a look at other APIs provided by FileUtils too other than readFileToString






      share|improve this answer


























        0












        0








        0







        I was facing same issue before I realized that the following way is only good for loading files in your classpath.



        InputStream is = Myclass.class.getClassLoader().getResourceAsStream("/config.properties");


        For picking files regardless of whether they are in classpath or not, I use Apache FileUtils. For e.g I used following and it worked like a charm.



        String str = FileUtils.readFileToString(new File(path_to_your_file), "UTF-8");
        IOUtils.toInputStream(str, "UTF-8"); // if you need InputStream object


        You can have a look at other APIs provided by FileUtils too other than readFileToString






        share|improve this answer













        I was facing same issue before I realized that the following way is only good for loading files in your classpath.



        InputStream is = Myclass.class.getClassLoader().getResourceAsStream("/config.properties");


        For picking files regardless of whether they are in classpath or not, I use Apache FileUtils. For e.g I used following and it worked like a charm.



        String str = FileUtils.readFileToString(new File(path_to_your_file), "UTF-8");
        IOUtils.toInputStream(str, "UTF-8"); // if you need InputStream object


        You can have a look at other APIs provided by FileUtils too other than readFileToString







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 9:14









        tryingToLearntryingToLearn

        2,58222750




        2,58222750























            0














            getResourceAsStream("config.properties") - this method will only work if your properties file is in the classpath. I think you have added the properties file outside the classpath. This method is basically used for accessing a file present in the classpath.



            Please check it again.



            Thanks :)



            Working Code :



            public class MyClass {

            public static void main(String args) throws Exception {
            Properties prop = new Properties();
            InputStream input = null;

            try {

            String filename = "config.properties";
            input = MyClass.class.getClassLoader().getResourceAsStream(filename);
            if (input == null) {
            System.out.println("Sorry, unable to find " + filename);
            return;
            }

            // load a properties file from class path, inside static method
            prop.load(input);

            // get the property value and print it out
            System.out.println(prop.getProperty("serverUrl"));

            } catch (IOException ex) {
            ex.printStackTrace();
            } finally {
            if (input != null) {
            try {
            input.close();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }
            }

            }

            }





            share|improve this answer


























            • how do I add the file to the classpath?

              – user840930
              Nov 16 '18 at 8:29






            • 1





              I'm pretty sure the file is being found now. I changed the code to getResourceAsStream("/config.properties");

              – user840930
              Nov 16 '18 at 10:00













            • keep the properties file in the resources folder of your project.. Then if you are using eclipse. Then right click on the project -> Properties ->Java Build Path -> click on source tab -> Click on Add folder and add the resources folder -> Click on Apply -> Click on Apply and close. Make a little modification to getResourceAsStream("resources/config.properties")

              – Anish B.
              Nov 16 '18 at 10:04













            • tried "resources/config.properties. That did not work. Can't access the file. NullPointerException. The correct way looks like "/config.properties"

              – user840930
              Nov 16 '18 at 11:06











            • then its fine :)

              – Anish B.
              Nov 16 '18 at 12:39
















            0














            getResourceAsStream("config.properties") - this method will only work if your properties file is in the classpath. I think you have added the properties file outside the classpath. This method is basically used for accessing a file present in the classpath.



            Please check it again.



            Thanks :)



            Working Code :



            public class MyClass {

            public static void main(String args) throws Exception {
            Properties prop = new Properties();
            InputStream input = null;

            try {

            String filename = "config.properties";
            input = MyClass.class.getClassLoader().getResourceAsStream(filename);
            if (input == null) {
            System.out.println("Sorry, unable to find " + filename);
            return;
            }

            // load a properties file from class path, inside static method
            prop.load(input);

            // get the property value and print it out
            System.out.println(prop.getProperty("serverUrl"));

            } catch (IOException ex) {
            ex.printStackTrace();
            } finally {
            if (input != null) {
            try {
            input.close();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }
            }

            }

            }





            share|improve this answer


























            • how do I add the file to the classpath?

              – user840930
              Nov 16 '18 at 8:29






            • 1





              I'm pretty sure the file is being found now. I changed the code to getResourceAsStream("/config.properties");

              – user840930
              Nov 16 '18 at 10:00













            • keep the properties file in the resources folder of your project.. Then if you are using eclipse. Then right click on the project -> Properties ->Java Build Path -> click on source tab -> Click on Add folder and add the resources folder -> Click on Apply -> Click on Apply and close. Make a little modification to getResourceAsStream("resources/config.properties")

              – Anish B.
              Nov 16 '18 at 10:04













            • tried "resources/config.properties. That did not work. Can't access the file. NullPointerException. The correct way looks like "/config.properties"

              – user840930
              Nov 16 '18 at 11:06











            • then its fine :)

              – Anish B.
              Nov 16 '18 at 12:39














            0












            0








            0







            getResourceAsStream("config.properties") - this method will only work if your properties file is in the classpath. I think you have added the properties file outside the classpath. This method is basically used for accessing a file present in the classpath.



            Please check it again.



            Thanks :)



            Working Code :



            public class MyClass {

            public static void main(String args) throws Exception {
            Properties prop = new Properties();
            InputStream input = null;

            try {

            String filename = "config.properties";
            input = MyClass.class.getClassLoader().getResourceAsStream(filename);
            if (input == null) {
            System.out.println("Sorry, unable to find " + filename);
            return;
            }

            // load a properties file from class path, inside static method
            prop.load(input);

            // get the property value and print it out
            System.out.println(prop.getProperty("serverUrl"));

            } catch (IOException ex) {
            ex.printStackTrace();
            } finally {
            if (input != null) {
            try {
            input.close();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }
            }

            }

            }





            share|improve this answer















            getResourceAsStream("config.properties") - this method will only work if your properties file is in the classpath. I think you have added the properties file outside the classpath. This method is basically used for accessing a file present in the classpath.



            Please check it again.



            Thanks :)



            Working Code :



            public class MyClass {

            public static void main(String args) throws Exception {
            Properties prop = new Properties();
            InputStream input = null;

            try {

            String filename = "config.properties";
            input = MyClass.class.getClassLoader().getResourceAsStream(filename);
            if (input == null) {
            System.out.println("Sorry, unable to find " + filename);
            return;
            }

            // load a properties file from class path, inside static method
            prop.load(input);

            // get the property value and print it out
            System.out.println(prop.getProperty("serverUrl"));

            } catch (IOException ex) {
            ex.printStackTrace();
            } finally {
            if (input != null) {
            try {
            input.close();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }
            }

            }

            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 '18 at 10:21

























            answered Nov 16 '18 at 7:56









            Anish B.Anish B.

            148




            148













            • how do I add the file to the classpath?

              – user840930
              Nov 16 '18 at 8:29






            • 1





              I'm pretty sure the file is being found now. I changed the code to getResourceAsStream("/config.properties");

              – user840930
              Nov 16 '18 at 10:00













            • keep the properties file in the resources folder of your project.. Then if you are using eclipse. Then right click on the project -> Properties ->Java Build Path -> click on source tab -> Click on Add folder and add the resources folder -> Click on Apply -> Click on Apply and close. Make a little modification to getResourceAsStream("resources/config.properties")

              – Anish B.
              Nov 16 '18 at 10:04













            • tried "resources/config.properties. That did not work. Can't access the file. NullPointerException. The correct way looks like "/config.properties"

              – user840930
              Nov 16 '18 at 11:06











            • then its fine :)

              – Anish B.
              Nov 16 '18 at 12:39



















            • how do I add the file to the classpath?

              – user840930
              Nov 16 '18 at 8:29






            • 1





              I'm pretty sure the file is being found now. I changed the code to getResourceAsStream("/config.properties");

              – user840930
              Nov 16 '18 at 10:00













            • keep the properties file in the resources folder of your project.. Then if you are using eclipse. Then right click on the project -> Properties ->Java Build Path -> click on source tab -> Click on Add folder and add the resources folder -> Click on Apply -> Click on Apply and close. Make a little modification to getResourceAsStream("resources/config.properties")

              – Anish B.
              Nov 16 '18 at 10:04













            • tried "resources/config.properties. That did not work. Can't access the file. NullPointerException. The correct way looks like "/config.properties"

              – user840930
              Nov 16 '18 at 11:06











            • then its fine :)

              – Anish B.
              Nov 16 '18 at 12:39

















            how do I add the file to the classpath?

            – user840930
            Nov 16 '18 at 8:29





            how do I add the file to the classpath?

            – user840930
            Nov 16 '18 at 8:29




            1




            1





            I'm pretty sure the file is being found now. I changed the code to getResourceAsStream("/config.properties");

            – user840930
            Nov 16 '18 at 10:00







            I'm pretty sure the file is being found now. I changed the code to getResourceAsStream("/config.properties");

            – user840930
            Nov 16 '18 at 10:00















            keep the properties file in the resources folder of your project.. Then if you are using eclipse. Then right click on the project -> Properties ->Java Build Path -> click on source tab -> Click on Add folder and add the resources folder -> Click on Apply -> Click on Apply and close. Make a little modification to getResourceAsStream("resources/config.properties")

            – Anish B.
            Nov 16 '18 at 10:04







            keep the properties file in the resources folder of your project.. Then if you are using eclipse. Then right click on the project -> Properties ->Java Build Path -> click on source tab -> Click on Add folder and add the resources folder -> Click on Apply -> Click on Apply and close. Make a little modification to getResourceAsStream("resources/config.properties")

            – Anish B.
            Nov 16 '18 at 10:04















            tried "resources/config.properties. That did not work. Can't access the file. NullPointerException. The correct way looks like "/config.properties"

            – user840930
            Nov 16 '18 at 11:06





            tried "resources/config.properties. That did not work. Can't access the file. NullPointerException. The correct way looks like "/config.properties"

            – user840930
            Nov 16 '18 at 11:06













            then its fine :)

            – Anish B.
            Nov 16 '18 at 12:39





            then its fine :)

            – Anish B.
            Nov 16 '18 at 12:39


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333386%2fjar-file-built-with-maven-cant-find-configuration-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)