I need to read a config file
I get this error while running the following code:
java.util.Properties$LineReader.readLine
The file config.cfg is present and has r/w permissions.
import java.util.*;
import java.util.Properties;
public class Config
{
Properties configFile;
public Config()
{
configFile = new java.util.Properties();
try {
configFile.load(this.getClass().getClassLoader().
getResourceAsStream("config.cfg"));
}catch(Exception eta){
eta.printStackTrace();
}
}
public String getProperty(String key)
{
String value = this.configFile.getProperty(key);
return value;
}
}
EDIT - Full Error
[java] java.lang.NullPointerException
[java] at java.util.Properties$LineReader.readLine(Properties.java:418)
[java] at java.util.Properties.load0(Properties.java:337)
[java] at java.util.Properties.load(Properties.java:325)
[java] at Config.<init>(Unknown Source)
[java] at ClosureBuilder.<clinit>(Unknown Source)
EDIT - Directory Structure
src
-> config.java
-> config.cfg
You have to put your config.cfg in the same folder where your .class file lies.
The resource stream is being returned as null. The resource isn't where you think it is in the classpath.
It seems your program is not able to find the config.cfg file.
this.getClass().getClassLoader().getResourceAsStream("config.cfg")
The above call is returning null.
Try the below:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.cfg")
if(is !=null){
configFile.load(is);
}
This change will not let your program fail. But also, if the file is not located, your property object configFile will not have any property from the file.
Check the path what you are getting for your config using this.getClass().getResource("/");
Related
am trying to use an xml file from outside jar.now its getting null pointer exception
private static String getRequestConfigurationLocation() throws UnsupportedEncodingException
{
URL resourceURL = ConfigurationFactory.class.getResource(Configuration.XML_CONF);
//Assert.notNull(resourceURL, "Resource url is null : ");
String urlFilePath = resourceURL.getFile();
String actualFilePath = java.net.URLDecoder.decode(urlFilePath, StandardCharsets.UTF_8.name());
return actualFilePath;
}
Configuration
public class ConfigurationFactory
{
public static final String XML_CONF = "D:/DEV//X/X1/Service/target/conf/rConfiguration.xml";
}
how can i use external file in an executable jar
error log
Caused by: java.lang.NullPointerException
You should use java.util.Properties to access external file,
refer to this answer to load xml properties file
In your property file try :
external.config=D:/DEV//X/X1/Service/target/conf/rConfiguration.xml
Or
annotate your configuration file with following
#configuration
#ImportResource("D:/DEV//X/X1/Service/target/conf/rConfiguration.xml")
I have build a ESAPITestValidator class as follows :
public class ESAPITestValidator {
Validator instance = ESAPI.validator();
ValidationErrorList errors = new ValidationErrorList();
File parent = null;
String path = new String("");
boolean status = false;
public boolean testGetValidDirectoryPath(String inputPath,String context)
{
try
{
parent = new File(inputPath).getParentFile().getCanonicalFile();
//String parent = ESAPI.securityConfiguration().getResourceFile("ESAPI.properties").getParentFile().getCanonicalPath();
path = instance.getValidDirectoryPath(context, inputPath, parent, true, errors);
status = true;
return status;
}catch(Exception exception)
{
exception.printStackTrace();
status= false;
}
return status;
}
}
Now I am Trying to invoke the testGetValidDirectoryPath() from a main class as follows :
public class HelloWorld {
public static void main(String[] args) {
ESAPITestValidator obj = new ESAPITestValidator();
if ( obj.testGetValidDirectoryPath("C:\\Users\\1730176\\Downloads","C:\\Users") )
System.out.println("Success");
else
System.out.println("Failure");
}
}
I have also Uploaded the ESAPI.properties and Validation.properties in the same Source folder as the main and validator class.
But I am getting the following exception :
System property [org.owasp.esapi.opsteam] is not set
System property [org.owasp.esapi.devteam] is not set
Attempting to load ESAPI.properties via file I/O.
Attempting to load ESAPI.properties as resource file via file I/O.
Not found in 'org.owasp.esapi.resources' directory or file not readable:
D:\Arindam_Workspace\ESAPI_Workspace\ESAPIProofOfConcept\ESAPI.properties
Not found in SystemResource Directory/resourceDirectory:
.esapi\ESAPI.properties
Found in 'user.home' directory: C:\Users\1730176\esapi\ESAPI.properties
Loaded 'ESAPI.properties' properties file
SecurityConfiguration for Validator.ConfigurationFile.MultiValued not found
in ESAPI.properties. Using default: false
Attempting to load validation.properties via file I/O.
Attempting to load validation.properties as resource file via file I/O.
Not found in 'org.owasp.esapi.resources' directory or file not readable:
D:\Arindam_Workspace\ESAPI_Workspace\ESAPIProofOfConcept\
validation.properties
Not found in SystemResource Directory/resourceDirectory:
.esapi\validation.properties
Found in 'user.home' directory: C:\Users\1730176\esapi\validation.properties
Loaded 'validation.properties' properties file
Exception in thread "main" java.lang.NoClassDefFoundError:
javax/servlet/http/HttpServletRequest
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getMethod0(Class.java:2670)
at java.lang.Class.getMethod(Class.java:1603)
at org.owasp.esapi.util.ObjFactory.make(ObjFactory.java:77)
at org.owasp.esapi.ESAPI.validator(ESAPI.java:191)
at main.utility.ESAPITestValidator.<init>(ESAPITestValidator.java:20)
at main.core.HelloWorld.main(HelloWorld.java:16)
Caused by: java.lang.ClassNotFoundException:
javax.servlet.http.HttpServletRequest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 8 more
Can anyone sujjest What I have done worng or am I missing something ?
The JVM is looking for the javax/servlet/http/HttpServletRequest class, and you haven't loaded it into the classpath. You will need to include an implementation of the java servlet API on the classpath. Keep in mind that ESAPI is a web-based library and you will probably have an easier time testing if you clone the actual esapi-java-legacy project and work with the unit tests there.
I want content of one .java file to the another .java file. I am doing this with FileInput/FileOutput Stream classes in eclipse IDE. I have put one file named FileToFile.java inside Nisarg/src/FiliIO(package).
And I am getting FileNotFoundException at line 12. I want to know why this exception raised?
This is what I actually got at runtime..
Exception in thread "main" java.io.FileNotFoundException: FileToFile.java (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at FileIO.FileToFile.main(FileToFile.java:12)
This is a piece of code:
package FileIO;
import java.io.*;
public class FileToFile {
/**
* #param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
FileInputStream i=new FileInputStream("FileToFile.java");// Current file content is wanted to be written...
FileOutputStream o=new FileOutputStream("M.java"); // Destination file which is also in same place.(Nisarg/src/FileIO(package)...
int a=0;
while((a=i.read())!=-1)
{
o.write((byte)a);
}
o.close();
i.close();
System.out.print("Done");
}
}
What should be done to achieve my requirements? I have searched but I was unable to where to put the file. Thank you in advance..!!
Java cant find your input file FileToFile.java, that's what is basically mean. You either can specify a absolute file path or find out what folder your main class is located at and place your file FileToFile.java there.
Find the current directory of your main class, use System.getProperty("user.dir")
package fileIO;
import java.io.*;
public class FileToFile {
public static void main(String[] args)throws IOException {
FileInputStream i=new FileInputStream("src\\fileIO\\FileToFile.java");
FileOutputStream o=new FileOutputStream("F:\\M.java");
int a=0;
while((a=i.read())!=-1)
{
o.write((byte)a);
}
o.close();
i.close();
System.out.print("Done");
}
}
You should have to use
1)the absolute path for your file
2)Find the current directory using System.getProperty("user.dir") and place your file there.
3)You can change your current working directory for your application by Go to
run configuration >> Arguments >> Other radio button. Then enter an absolute path name as the working directory for the launched application.Place your file in the specified directory.
This question already has answers here:
Where to place and how to read configuration resource files in servlet based application?
(6 answers)
Closed 6 years ago.
Hi all i am getting the error
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at richard.fileupload.FileUploadController.loadProp(FileUploadController.java:48)
... 60 more
whenever my properties class is called, this is the code
private Properties configProp = new Properties();
#PostConstruct
public void loadProp() {
System.out.println("Loading properties");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("../resources/config.properties");
try {
configProp.load(in);
System.out.println(configProp.getProperty("destinationPDF"));
System.out.println(configProp.getProperty("destination"));
System.out.println(configProp.getProperty("fileList"));
} catch (IOException e) {
e.printStackTrace();
}
}
private String destinationPDF = configProp.getProperty("destinationPDF");
public String destination = configProp.getProperty("destination");
private String username;
the error seems to be coming from this line :
InputStream in = this.getClass().getClassLoader().getResourceAsStream("../resources/config.properties");
try {
configProp.load(in);
what is causing this error and how can i solve it ?
the code above, should point to the properties file and then retrive the three variables values from it, it is not getting as far as the
System.out.println(configProp.getProperty("destinationPDF"));
System.out.println(configProp.getProperty("destination"));
System.out.println(configProp.getProperty("fileList
before i get the error
this is my directory structure
as you can see the properties file is in resources/, how would i get a link to this
EDIT :
ok so it works perfectly fine with the full link to the file :
configProp.load(new FileInputStream("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/WEB-INF/config.properties"));
but no matter where i put the file in the projet i can not get it to load at all, why is this ?
This line doesn't make sense:
this.getClass().getClassLoader().getResourceAsStream("../resources/config.properties")
ClassLoader.getResourceAsStream() loads a resource from the classpath, using a package path starting at the root package. And there is nothing above the root package.
I am trying to read a config file based on the code here:
http://www.opencodez.com/java/read-config-file-in-java.htm
So I found that if the config.cfg is in same directory as of where I am running the code, then everything is fine but if the config is at different directory
example: /path/to/config.cfg
I get this error:
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at packagename.conf.Config.<init>(Config.java:14)
at packagename.conf.Config.main(Config.java:30)
My guess is it is not able to find the file.
But how do I modify the above code to read config file from a different folder?
Thanks
Edit: Code from the link:
import java.util.*;
import java.util.Properties;
public class Config
{
Properties configFile;
public Config()
{
configFile = new java.util.Properties();
try {
configFile.load(this.getClass().getClassLoader().
getResourceAsStream("myapp/config.cfg"));
}catch(Exception eta){
eta.printStackTrace();
}
}
public String getProperty(String key)
{
String value = this.configFile.getProperty(key);
return value;
}
}
The code that you posted expects the configuration-file to be on the classpath (that is, in the same sorts of places that Java looks for your .class files). So, you can either include the directory containing the configuration-file in the classpath:
java -classpath .:/path/to packagename.conf.Config
Or else you can modify the code to expect the configuration-file to be a regular filesystem file:
final InputStream cfg = new FileInputStream("/path/to/config.cfg");
try
{ configFile.load(cfg); }
finally
{ cfg.close(); }