powershell run java process problem

2019-02-10 03:43发布

I'm trying to run a java process via Powershell in Windows XP. Here's the command:

java.exe -cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install

So, the classpath is . and .\common.jar (I think java takes the wrong slashes, right?) There are two environment variables, one "atest1" the other "." and the class to execute main on is DW_Install (in the default package).

This command works in cmd.exe, but doesn't is PS. What's going on? What is PS doing while parsing this command that CMD doesn't do (or vice versa)?

Aaron

5条回答
beautiful°
2楼-- · 2019-02-10 03:55
start-process -nnw java "-cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install"
查看更多
走好不送
3楼-- · 2019-02-10 03:57

The problem is that PS for some reason parses -Dresourcepath=. differently than cmd. What works is

java -cp '.;.\common.jar' -Dcontext=atest1 "-Dresourcepath=." DW_Install

It doesn't matter which way the slash goes, and it doesn't matter which quotes one uses (' or "). The classpath must be escaped, however, with some kind of quotes. A good test to see what's getting by the PS interpreter is to echo it. The following:

echo java -cp '.;.\common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install

yields the following output:

java
-cp
.;.\common.jar
-Dcontext=etaste1
-Dresourcepath=
.
DW_Install

(Notice the resourcepath and the value of resourcepath are not on the same line.) Whereas the output to

echo java -cp '.;.\common.jar' -Dcontext=atest1 '-Dresourcepath=.' DW_Install

yields the following output:

java
-cp
.;.\common.jar
-Dcontext=etaste1
-Dresourcepath=.
DW_Install

Which is much more to our liking.

Although I wish this upon none of you, I hope that this post helps those of you that must deploy java projects on Windows machines (even though they will not run on any other platform ever).

查看更多
该账号已被封号
4楼-- · 2019-02-10 03:58

Another example based on https://gaming.stackexchange.com/questions/24543/how-do-i-change-player-name-in-minecraft-multiplayer-in-offline-mode-in-linux

function mineCraftAs {
    Param (
        [parameter(mandatory=$true, HelpMessage="Minecraft character name." ,ValueFromPipeline=$true)]
        [string] $name
    )
    if(!(test-path $env:appdata)) { $(throw "Appdata not found at $env:appdata")}
    $private:minecraftPath=Join-Path $env:appdata .minecraft
    if(!(test-path $minecraftPath)) { $(throw "Minecraft not found at $minecraftpath")}
    $private:minebinPath=join-path $minecraftPath "bin"
    if(!(test-path $minebinPath)) { $(throw "Minecraft bin not found at $minebinPath")}

    $minebinPath | write-debug
    gci $minebinpath | write-debug

    #java -Xms512m -Xmx1024m -cp "%APPDATA%/.minecraft\bin\*" -Djava.library.path="%APPDATA%\.minecraft\bin\natives" net.minecraft.client.Minecraft '"'%1'"'

    echo java -Xms512m -Xmx1024m  -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)

    $minecraftJob=& 'C:\Program Files (x86)\Java\jre6\bin\java.exe' -Xms512m -Xmx1024m  -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)
}
minecraftas newbie
查看更多
叛逆
5楼-- · 2019-02-10 03:59

Running external command-line programs from PowerShell is sometimes a bit problematic because there PowerShell exposes two different parsing modes that get trumped by the different syntaxes of said external programs.

In any case, running a command in Powershell requires using either the . prefix (dot-"sourcing") or the & operator.

You can workaround this by passing each parameter to the external program as separate variables, like so:

PS> $classpath = ".;./common.jar"
PS> $env = "-Dcontext=atest1 -Dresourcepath=."
PS> $class = "DW_Install"

PS> . java.exe -cp $classpath $env $class
查看更多
Rolldiameter
6楼-- · 2019-02-10 04:17

The following should work:

java.exe -cp '.;./common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install

I guess that PowerShell interprets the ; in the classpath as command delimiter, thereby trying to run java -cp . and ./common.jar -D....

查看更多
登录 后发表回答