Search code examples
javapowershellargumentswlst

run java.exe with arguments from powershell


I'm trying to call wlst/jython/python from powershell

set classpath with setWLSEnv.cmd is not set in the right session? so I have tried to set -cp as argument

& C:\bea\tpc\weblogic1033\server\bin\setWLSEnv.cmd; 
$cp='C:\bea\tpc\WEBLOG~1\server\lib\weblogic.jar'
$wlst='weblogic.WLST'
$script='C:\domains\tpc\Domain\bin\status.py'
$java="C:\PROGRA~1\Java\JROCKI~1.0\bin\java"
& "$java $cp $wlst $script"
#or
. "`"$java`" -cp `"$cp`" $wlst `"$script`""
#or
& "`"$java`" -cp `"$cp`" $wlst `"$script`""

I have tried to quote the command string in various ways without success

The term '"C:\PROGRA~1\Java\JROCKI~1.0\bin\java" -cp "C:\bea\tpc\WEBLOG~1\server\lib\weblogic.jar" weblogic.WLST "C:\domains\tpc\SasTT pcDomain\bin\status.py"' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:_WORK_\SAS\statusAll.ps1:15 char:2 + . <<<< ""$java" -cp "$cp" $wlst "$script"" + CategoryInfo : ObjectNotFound: ("C:\PROGRA~1\Ja...\bin\status.py":String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException


Solution

  • When you use the call operator &, the next token needs to be the name of a command and nothing else. So instead of this:

    & "$java $cp $wlst $script"
    

    Try this:

    & $java $cp $wlst $script
    

    Sometimes getting arguments to native exes can get ugly. A technique that usually works but is unsafe if any of your arguments come from user input is this:

    Invoke-Expression "$java $cp $wlst $script"