0

Recently I've started fiddling around with PowerShell, and I bumped into an issue with running .jar files. Simply put, I'm using plantuml and I'd like to simply have a "plantuml" command to run it. Normally, running the program would be done by typing. java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar. This is of course quite a handful, and I'd like to shorten this to simply plantuml.

My current work-around is the following function: function plantuml($UmlPath, $ImgPath) { java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar $UmlPath $ImgPath } However, I cannot pass any parameters to the .jar file like this, because Powershell intercepts them and interprets them as function parameters. A current workaround for this is by wrapping them in quotation marks, but I find this ugly and I often forget.

Is there any way to simply be able to type plantuml so that PowerShell expands it to java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar? The only similar question I found was this one, but it doesn't appear to have an actual answer.

PostLee
  • 3
  • 2
  • if you need to pass additional parameters you can add an additional param to your function, something like `$params` and send it a string representing the parameters you want added to the end of the command, would end up with a bit of a strange syntax but as long as you were aware of it it would at least save you a bunch of typing. – Mike Garuccio Dec 02 '16 at 19:18
  • The advice in this article may be helpful to you: [Windows IT Pro - Running Executables in PowerShell](http://windowsitpro.com/powershell/running-executables-powershell). – Bill_Stewart Dec 02 '16 at 21:41
  • Also, please give an example of what you mean by "I cannot pass any parameters to the .jar file like this, because Powershell intercepts them and interprets them as function parameters." – Bill_Stewart Dec 02 '16 at 21:44
  • Example: a way to test whether plantuml is working is by passing the -testdot parameter to the .jar, e.g. `java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar -testdot`. However, if I do this with the current function, PowerShell sees "-testdot" as a parameter to my actual function instead of to the .jar file, so that the .jar file never actually receives the parameter. – PostLee Dec 02 '16 at 22:26
  • Thank you Bill_Stewart, a combination of your article and TesselatingHeckler's answer solved my issue! – PostLee Dec 02 '16 at 22:51

1 Answers1

0

I don't have plantuml or anything similar to test with, but you can get all the parameters passed to a function with the $args variable, so this approach might work:

function plantuml {

  # Array of your default arguments to Java.exe to start plantuml
  $arguments = @('-jar',
                 'C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar') 

  # All arguments passed to this function, umlpath, imgpath, and anything else
  # are in the special variable $args, add those into the array as well.
  $arguments += $args

  Start-Process Java -ArgumentList $arguments
}
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • I think this answer is going in the right direction! However, the expected behaviour of running plantuml -testdot would be that the output is in the PowerShell terminal itself, whereas now it appears to open a new terminal for the Java process, after which it immediately closes again. The biggest downside of this is that I can't see the printed result, because the window immediately closes. Any ideas? – PostLee Dec 02 '16 at 22:36
  • Update: passing the -NoNewWindow parameter fixed that, but now the terminal doesn't wait for plantuml to finish and the output is in a weird place (i.e. if I start writing, I overwrite the output). I tried adding -Wait, but that didn't seem to have any effect. *EDIT*: Please ignore the above, the -Wait parameter suddenly worked, guess I forgot to reload my PowerShell. Thank you for your help! – PostLee Dec 02 '16 at 22:47
  • @postlee - NoNewWindow is a good catch, and I'm glad you got -Wait working or I'd have had no clue what else to suggest for that :) – TessellatingHeckler Dec 04 '16 at 00:18