ha9u63a7
ha9u63a7

Reputation: 6862

Run a Java Jar from Powershell

I checked quite a lot of other answers for this question on SO, but none of them really seem to work consistently, and correctly. Also, the examples weren't fitting my use case.

I have a java application jar which has a main class. I want to run it with the following arguments:

-Xms1300m -Xmx1300m -classpath "myClassPath;anotherone;anotherOne" -Xdebug -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

I am always getting simply the following when I try to run it using the following command on my powershell script (not command line)

Start-Process java -ArgumentList '-Xms1300m -Xmx1300m -classpath "myClassPath;anotherone;anotherOne" -Xdebug -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 the.jar.in.myClassPath.mainClass startArgs

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName
-------  ------    -----      ----- -----   ------     --  -- -----------
      6       2      204        704     7     0.00   3588   1 java

My java home path is correctly set in environment variables so there's nothing wrong there.

I ran an echo of the argument set and they look correct. I am kind of stuck here to figure out why this causing a problem? Powershell is more complicated as opposed to DOS because when I run it in DOS Command Prompt, it just works. So what has gone wrong for me here?

Regards,

Upvotes: 1

Views: 7641

Answers (1)

Bill_Stewart
Bill_Stewart

Reputation: 24585

Why Start-Process? Just run the command, quoting the needed parameters. You should be able to just run it this way:

java -Xms1300m -Xmx1300m -classpath "myClassPath;anotherone;anotherOne" -Xdebug "-Djava.compiler=NONE" "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"

Notes:

  1. Quote the the argument for -classpath since it contains ; characters.

  2. Quote -D and its attached argument because it contains the . character.

  3. Quote -X and its attached argument because it contains , and = characters.

Basically: Quote a parameter that contains characters that are otherwise syntactically meaningful to PowerShell.

You can troubleshoot executable parameter passing using a handy program I wrote called showargs.exe, which you can get from downloading the code associated with the following article:

IT Pro Today - Running Executables in PowerShell

Upvotes: 2

Related Questions