Skip to content

Instantly share code, notes, and snippets.

@mike10004
Created September 1, 2016 16:48
Show Gist options
  • Save mike10004/bf6ce365b6949b1a5a587fc5f3d332f8 to your computer and use it in GitHub Desktop.
Save mike10004/bf6ce365b6949b1a5a587fc5f3d332f8 to your computer and use it in GitHub Desktop.
Program that prints Java system properties; can be run interactively
import java.util.Properties;
import java.util.Scanner;
public class SysPropsTool {
public static final String QUIT = "/Q";
public static final String QUIT_ = "/QUIT";
public static final String HELP = "/?";
public static final String HELP_ = "/H";
public static final String HELP__ = "/HELP";
public static final String LIST = "/LS";
public static final String LIST_ = "/LIST";
public static final String LISTALL = "/ALL";
public static final String PROPS = "props >>>";
static Properties props = System.getProperties();
public static void main (String[] args) {
if (args.length > 0) {
for (String arg : args) {
String preStic = arg.trim().toUpperCase();
if (!react(preStic, arg)) {
System.exit(0);
}
}
} else {
usageInfo();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print(PROPS);
try {
String line = sc.nextLine();
if (line == null || line.length() < 1) {
continue;
}
String preStic = line.trim().toUpperCase();
boolean keepGoing = react(preStic, line);
if (!keepGoing) {
break;
}
} catch (Exception ex) {
System.err.println(ex.toString());
usageInfo();
}
}
}
}
static boolean react(String preStic, String line) {
if (QUIT.equals(preStic) || QUIT_.equals(preStic)) {
return false;
}
if (HELP.equals(preStic) || HELP_.equals(preStic) || HELP__.equals(preStic)) {
usageInfo();
return true;
}
boolean isList = LIST.equals(preStic) || LIST_.equals(preStic) || LISTALL.equals(preStic);
if (isList) {
showAll();
}else {
showIndivl(line);
}
return true;
}
static void showAll () {
if (props != null) {
props.list(System.out);
}
}
static void showIndivl (String key) {
String value = props.getProperty(key, "[NAN]");
StringBuilder info = new StringBuilder(key);
info.append("=").append(value);
System.out.println(info);
}
public static String get (String key) {
return props.getProperty(key);
}
public static Properties set (String key, String value) {
props.setProperty(key, value);
return props;
}
static void usageInfo () {
System.out.println("[Usage]: \n\t [/q|/quit]:\t to quit \n\t [/ls|/list|/all]:\t to list all props \n\t {key}:\t get the key-value \n\t [/h|/?|/help]:\t for help");
}
}
@mike10004
Copy link
Author

I didn't write the original version of this, but I have no idea where it came from. The original version only ran in interactive mode, and I added a feature where the user specifies a command or property name on the command line and the program reacts to it in the way it would have in interactive mode, then exits.

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