I have a global.properties file and have to define the file path inside this properties file.
SheetPath=C:\\Users\\test\\Automation-Scripts\\DataTable.xlsx
This is an absolute path but require a way to define a relative path that can be consumed while calling.
Properties file:
testPath=API_Files/duplicateToken.json
Load the properties file:
public static Properties readProperties = new Properties();
public static void loadPropertiesFile() {
File propertiesFile = new File(location of properties file);
try {
FileInputStream fileInput = new FileInputStream(propertiesFile);
readProperties.load(fileInput);
} catch (Exception e) {
Logger.LogError("Error in loading the Properties file" + e.getMessage());
}
}
Read the properties file and get absolute path:
String testPath = readProperties.getProperty("testPath").trim();
File absolutePath = new File(System.getProperty("user.dir") + testPath);
System.out.println(absolutePath);
Sample output:
C:\Users\test\Automation-Scripts\duplicateToken.json
The properties file should be relative to classpath. You can create a "configs" folder at the level of src and use the below code to read the file. Refer this for more explanation and techniques.
private Properties properties;
private final String propertyFilePath= "configs//Configuration.properties";
BufferedReader reader = new BufferedReader(new FileReader(propertyFilePath));
Properties properties = new Properties();
properties.load(reader);
Related
I'm executing a jar file which reads configs from a config file outside of /home/user/xxx/testFolder/jarfile, the path of config file is /opt/xxx/conf/global_config.cfg.
However, I'm able to access files inside the jar, so I assume the error is due to the file not being found.
Below is my code:
public Properties createProperties(){
Properties p = null;
ClassLoader cl = this.getClass().getClassLoader();
try (InputStream stream = cl.getResourceAsStream("/opt/xxx/conf/global_config.cfg")) {
p = new Properties();
BufferedInputStream bis = new BufferedInputStream(stream);
p.load(bis); // this is throwing the error
System.out.println(p.toString());
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
What is the correct way of getting a file regardless of its path in a Linux system?
cl.getResourceAsStream("/opt/xxx/conf/global_config.cfg")
expects the resource to be available in relation to the class location. So, it will search as a relative path to the class inside the JAR. But the path /opt/xxx/conf/global_config.cfg is a absolute disk path, and for reading it , you need to use the FileInputStream
public Properties createProperties(){
Properties p = null;
ClassLoader cl = this.getClass().getClassLoader();
try (InputStream stream =new FileInputStream("/opt/xxx/conf/global_config.cfg")) {
p = new Properties();
p.load(stream);
System.out.println(p.toString());
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
I will use user.properties to overwrite some properties in jmeter.properties.
Overwriting the properties summariser.out in jmeter.properties:
in jmeter.properties
summariser.out=true
in user.properties
summariser.out=false
In the apache doc is written:
Note: You can define additional JMeter properties in the file defined
by the JMeter property user.properties which has the default value
user.properties. The file will be automatically loaded if it is found
in the current directory or if it is found in the JMeter bin
directory. Similarly, system.properties is used to update system
properties.
so, my user.properties is in /bin and I the property in jmeter.properties -> user.properties=user.properties.
I tried also to load manually like:
Properties props = new Properties();
InputStream is = getTempInputStream(userPropTempFilePath);
props.load(is);
is.close();
That all has no effect.
Some idea how to load user.properties in java and to check if the properties are loaded?
Thats the solution:
String userProp = JMeterUtils.getPropDefault("user.properties", "");
if (userProp.length() > 0) {
FileInputStream fis = null;
try {
File file = JMeterUtils.findFile(userProp);
if (file.canRead()) {
log.info("Loading user properties from: "
+ file.getCanonicalPath());
fis = new FileInputStream(file);
Properties tmp = new Properties();
tmp.load(fis);
jmeterProps.putAll(tmp);
LoggingManager.setLoggingLevels(jmeterProps);//Do what would be done earlier
}
} catch (IOException e) {
log.warn("Error loading user property file: " + userProp, e);
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
log.warn("There was problem closing file stream", ex);
}
}
}
Where can I place configuration file in Android project? Currently I am made a directory in res directory as :
res/config/configuration.properties
and want to access it as :
properties = new Properties();
properties.load(getClass().getResourceAsStream("config/configuration.properties"));
It is not working : input stream is null
You can place it in assets directory as assets/configuration.properties
Example code
try {
InputStream is = context.getAssets().open("configuration.properties");
Properties props = new Properties();
props.load(is);
String value = props.getProperty("key", "");
is.close();
} catch (Exception e) {
}
Properties properties = new Properties();
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("config.properties");
properties.load(inputStream);
properties.getProperty(key);
I cant understand what the error of this code.
public void run(String url) {
try {
FileInputStream file;
file = new FileInputStream(this.getClass().getResource(url));
Player p = new Player(file);
p.play();
}catch(Exception e){
System.err.print( url + e);
}
}
when i try to run it, it says me "no suitable constructor found for FileInputStream(URL)". Why its happening?
Use:
getClass().getResourceAsStream(classpathRelativeFile) for classpath resources
new FileInputStream(pathtoFile) for file-system resources.
Put the file in root of folder of your class path (folder where your .class files are generated) and then use statements below:
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream(filePath);
Player p = new Player(inputStream );
Here filePath is the relative file path w.r.t. the root folder.
It is simpler to use getResourceAsStream
InputStream in = getClass().getResourceAsStream(url);
Player p = new Player(file);
The parameter of FileInputStream constructor is File, String ... (see http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html ), but Class.getResource return URL (see http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html), not File, or String.
Try to use
public void run(String url) {
try {
FileInputStream file;
file = new FileInputStream(new File(this.getClass().getResource(url).toURI()));
Player p = new Player(file);
p.play();
}catch(Exception e){
System.err.print( url + e);
}
}
I’m creating an executable JAR that will read in a set of properties at runtime from a file. The directory structure will be something like:
/some/dirs/executable.jar
/some/dirs/executable.properties
Is there a way of setting the property loader class in the executable.jar file to load the properties from the directory that the jar is in, rather than hard-coding the directory.
I don't want to put the properties in the jar itself as the properties file needs to be configurable.
Why not just pass the properties file as an argument to your main method? That way you can load the properties as follows:
public static void main(String[] args) throws IOException {
Properties props = new Properties();
props.load(new BufferedReader(new FileReader(args[0])));
System.setProperties(props);
}
The alternative: If you want to get the current directory of your jar file you need to do something nasty like:
CodeSource codeSource = MyClass.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
File jarDir = jarFile.getParentFile();
if (jarDir != null && jarDir.isDirectory()) {
File propFile = new File(jarDir, "myFile.properties");
}
... where MyClass is a class from within your jar file. It's not something I'd recommend though - What if your app has multiple MyClass instances on the classpath in different jar files (each jar in a different directory)? i.e. You can never really guarantee that MyClass was loaded from the jar you think it was.
public static void loadJarCongFile(Class Utilclass )
{
try{
String path= Utilclass.getResource("").getPath();
path=path.substring(6,path.length()-1);
path=path.split("!")[0];
System.out.println(path);
JarFile jarFile = new JarFile(path);
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
if (entry.getName().contains(".properties")) {
System.out.println("Jar File Property File: " + entry.getName());
JarEntry fileEntry = jarFile.getJarEntry(entry.getName());
InputStream input = jarFile.getInputStream(fileEntry);
setSystemvariable(input);
InputStreamReader isr = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
System.out.println("Jar file"+line);
}
reader.close();
}
}
}
catch (Exception e)
{
System.out.println("Jar file reading Error");
}
}
public static void setSystemvariable(InputStream input)
{
Properties tmp1 = new Properties();
try {
tmp1.load(input);
for (Object element : tmp1.keySet()) {
System.setProperty(element.toString().trim(),
tmp1.getProperty(element.toString().trim()).trim());
}
} catch (IOException e) {
System.out.println("setSystemvariable method failure");
}
}