Skip to content

Instantly share code, notes, and snippets.

@vcabral19
Last active July 1, 2021 20:59
Show Gist options
  • Save vcabral19/d05234ea35541e78a252a68f9cab86d6 to your computer and use it in GitHub Desktop.
Save vcabral19/d05234ea35541e78a252a68f9cab86d6 to your computer and use it in GitHub Desktop.
playing with variables in java
package com.myproject;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class EnvVarTest {
public final Properties properties;
public EnvVarTest(){
this.properties = new Properties();
String propertiesFileName = "config.properties";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFileName);
try{
this.properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getEnvVarOrProperty(String envVarName){
String systemEnvVar = System.getenv(envVarName);
if (systemEnvVar == null){
systemEnvVar = properties.getProperty(envVarName);
}
return systemEnvVar;
}
public static void main(String[] args){
String eventsSourcePath;
String processedRegisteredEventsPath;
String processedAppLoadedEventsPath;
String sparkMasterConfig;
EnvVarTest envVarTest = new EnvVarTest();
eventsSourcePath = envVarTest.getEnvVarOrProperty("EVENTS_SOURCE_PATH");
processedRegisteredEventsPath = envVarTest.getEnvVarOrProperty("PROCESSED_REGISTERED_EVENTS_PATH");
processedAppLoadedEventsPath = envVarTest.getEnvVarOrProperty("PROCESSED_APPLOADED_EVENTS_PATH");
sparkMasterConfig = envVarTest.getEnvVarOrProperty("SPARK_MASTER");
System.out.println("EVENTS_SOURCE_PATH -> " + eventsSourcePath);
System.out.println("PROCESSED_REGISTERED_EVENTS_PATH -> " + processedRegisteredEventsPath);
System.out.println("PROCESSED_APPLOADED_EVENTS_PATH -> " + processedAppLoadedEventsPath);
System.out.println("SPARK_MASTER -> " + sparkMasterConfig);
}
}
@vcabral19
Copy link
Author

Protip: config.properties file should be in the "resources" folder of the project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment