简体   繁体   English

使用Powershell中的参数运行java.exe

[英]run java.exe with arguments from powershell

I'm trying to call wlst/jython/python from powershell 我正在尝试从Powershell调用wlst / jython / python

set classpath with setWLSEnv.cmd is not set in the right session? 在正确的会话中未设置setWLSEnv.cmd设置类路径? so I have tried to set -cp as argument 所以我试图将-cp设置为参数

& 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. 术语““ 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“'无法识别为cmdlet,函数,脚本文件或可运行程序的名称。 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 + . 在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 <<<<“ "$java “ -cp "$cp “ $ wlst "$script “” + CategoryInfo:ObjectNotFound:(“ C:\\ PROGRA〜1 \\ Ja ... \\ bin \\ status.py”:String) [],CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException

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. 有时,获取本机exe的参数可能会很丑陋。 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"

In addition to the trouble with how you're formatting your command into a string, setWLSEnv.cmd is a script for the Windows Command Prompt. 除了麻烦如何将命令格式化为字符串外, setWLSEnv.cmd是Windows命令提示符的脚本。 PowerShell cannot execute this file; PowerShell无法执行该文件; it does not know how to interpret it, much like how Notepad does not know how to interpret a docx file. 它不知道如何解释它,就像记事本不知道如何解释docx文件一样。 Windows associates .cmd files with the command prompt, so your first line is equivalent to Windows将.cmd文件与命令提示符关联,因此您的第一行等效于

& cmd.exe /c C:\bea\tpc\weblogic1033\server\bin\setWLSEnv.cmd

(Note that the semicolon is unnecessary since you don't have any other commands on the same line.) (请注意,不需要分号,因为同一行上没有其他命令。)

This creates a new process using cmd.exe. 这将使用cmd.exe创建一个新进程。 This new process executes the batch file, setting the environment variables you expect, and then the process exits, discarding the environment changes. 此新过程将执行批处理文件,设置所需的环境变量,然后该过程退出,放弃环境更改。

If you need setWLSEnv.cmd to set environment variables prior to executing your program, you should write a batch file that calls it instead of a PowerShell script. 如果在执行程序之前需要setWLSEnv.cmd设置环境变量,则应编写一个调用它的批处理文件而不是PowerShell脚本。 Otherwise, you will need to find or write a PowerShell equivalent to set up your environment. 否则,您将需要查找或编写等效的PowerShell来设置您的环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM