jar file built with maven can't find configuration file
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
add a comment |
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
you should add the error that you are getting
– Nawnit Sen
Nov 16 '18 at 7:43
TryMyclass.class.getClassLoader().getResourceAsStream("config.properties")
or put your config in same package asMyClass
.
– 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
add a comment |
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
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
java maven jar
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
TryMyclass.class.getClassLoader().getResourceAsStream("config.properties")
or put your config in same package asMyClass
.
– 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
add a comment |
you should add the error that you are getting
– Nawnit Sen
Nov 16 '18 at 7:43
TryMyclass.class.getClassLoader().getResourceAsStream("config.properties")
or put your config in same package asMyClass
.
– 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
add a comment |
3 Answers
3
active
oldest
votes
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
}
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
add a comment |
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
add a comment |
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();
}
}
}
}
}
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
|
show 2 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
}
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
add a comment |
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
}
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
add a comment |
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
}
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
}
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 19 '18 at 9:14
tryingToLearntryingToLearn
2,58222750
2,58222750
add a comment |
add a comment |
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();
}
}
}
}
}
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
|
show 2 more comments
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();
}
}
}
}
}
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
|
show 2 more comments
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();
}
}
}
}
}
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();
}
}
}
}
}
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
|
show 2 more comments
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
|
show 2 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 asMyClass
.– 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