I have to design a java console application which accepts a runtime parameter as an argument to the main method. The application will be eventually used by multiple scripts. I would prefer to avoid using a property file from which to read this parameter as it's possible that the parameter value is determined by the initial part of the script. Also, this parameter will be never changed by the application, but will only be used.
To quote an example of such a parameter, consider application user name. This user name will be used by the application in various outputs it generates. So this parameter would need to be used by multiple classes in the application.
What's the best way I can make this runtime parameter available for other classes in the application?
One of the consideration I have is to use a public static variable to store this. Thus it can be accessed anywhere. However, I am not sure if that's the right approach to do this.
public class ApplicationMain {
public static void main(String[] args) {
String param = args[0];
ApplicationConstants.PARAM = param;
}
}
public class Consumer {
public void generateReport() {
System.out.println("Reports Generated based on " + ApplicationConstants.PARAM);
}
}
Edit: The solution I need can not have any database or web application environments. It's a stand alone Java application which will be bundled as a jar file and need to be portable to any user's machine. Like a utility.
Aucun commentaire:
Enregistrer un commentaire