Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell run java process problem

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

like image 995
lmat - Reinstate Monica Avatar asked Jan 13 '11 20:01

lmat - Reinstate Monica


People also ask

How to Stop a process using PowerShell?

To stop a process by its ID, use taskkill /F /PID <PID> , such as taskkill /F /ID 312 7 if 3127 is the PID of the process that you want to kill. To stop a process by its name, use taskkill /IM <process-name> /F , for example taskkill /ID mspaint.exe /F .


2 Answers

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=atest1
-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).

like image 172
lmat - Reinstate Monica Avatar answered Sep 19 '22 15:09

lmat - Reinstate Monica


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
like image 30
Maxime Labelle Avatar answered Sep 20 '22 15:09

Maxime Labelle