Assessing environment variables in a command executed by Java Runtime.exec ()

I have scripts where I have a Java agent that runs on several platforms (specifically Windows, Solaris, and AIX). I would like to talk about differences in the structure of the file system using environment variables in the executable command line.

As far as I can tell, there is no way to get a method Runtime.exec()for solving / evaluating any environment variables referenced by the String command (or an array of strings).

I know that if push stumbles, I can write code to preprocess the String (s) command and manually enable environment variables (using getEnv(), etc.). However, I am wondering if there is a more reasonable way to do this, as I am sure that I am not the only person who wants to do this, and I am sure that there are “pitfalls” of my own implementation.

Your recommendations and suggestions are welcome.

edit I would like to refer to environment variables on the command line using some consecutive notation such as $ VAR and / or% VAR%. Not fussed that.

edit To be clear, I would like to execute a command, for example:

perl $SCRIPT_ROOT/somePerlScript.pl args

Windows Unix Runtime.exec(). , , -, , (/home/username/scripts vs C:\foo\scripts). , .

.

+3
4

, . , Java, , , , " ".

, . , , . , (, exec Unix) .

, Java 5 . , Java, , - ${var} /. - :

    private static String expandCommandLine(final String cmd) {
        final Pattern vars = Pattern.compile("[$]\\{(\\S+)\\}");
        final Matcher m = vars.matcher(cmd);

        final StringBuffer sb = new StringBuffer(cmd.length());
        int lastMatchEnd = 0;
        while (m.find()) {
            sb.append(cmd.substring(lastMatchEnd, m.start()));
            final String envVar = m.group(1);
            final String envVal = System.getenv(envVar);
            if (envVal == null)
                sb.append(cmd.substring(m.start(), m.end()));
            else
                sb.append(envVal);
            lastMatchEnd = m.end();
        }
        sb.append(cmd.substring(lastMatchEnd));

        return sb.toString();
    }
+6

, ? -D , System.getProperty

+1

[$]\\{(\\S+)\\}

(S+), xx ${a} xx ${b} xx A} xx ${B A B. - vars cmd?

, , .

[$]\\{(\\S+?)\\}
+1

, Windows ( -), , . , , (, , ).

int from      = 0;
int startperc = -1;
int endperc   = -1;

while (true) {
    int index = tok.indexOf("%", from);
    if (index == -1) break;
    if (startperc == -1) {
        startperc = index;
    } else if (endperc == -1) {
        endperc = index;
    }
    from = index + 1;

    if (startperc >= 0 && endperc > startperc) {
        String startbit = tok.substring(0, startperc);
        String middlebit = System.getenv(tok.substring(startperc + 1, endperc));
        String endbit = endperc <= tok.length() ? tok.substring(endperc + 1) : ""; // substr up to end

        // integrate
        tok = startbit + middlebit + endbit;

        // reset
        startperc = -1;
        endperc   = -1;
    }
}
0

Source: https://habr.com/ru/post/1729008/


All Articles