2016-12-02 65 views
0

最近我开始摆弄PowerShell,我遇到了运行.jar文件的问题。简而言之,我使用的是plantuml,我只想简单地使用“plantuml”命令来运行它。通常,运行程序将通过键入完成。 java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar。这当然是相当少的,我想简化为plantumlPowerShell中类C的宏扩展

我目前的解决办法是以下功能: function plantuml($UmlPath, $ImgPath) { java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar $UmlPath $ImgPath } 但是,我不能传递任何参数,以这样的.jar文件,因为PowerShell的拦截他们,并解释它们作为函数的参数。目前的解决方法是将它们包裹在引号中,但我觉得这很丑陋,我经常忘记。

有什么办法可以简单地键入plantuml以便PowerShell将其扩展到java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar?我发现的唯一类似问题是this one,但它似乎没有真正的答案。

+0

如果你需要传递额外的参数,你可以添加一个额外的参数给你的函数,像'$ params',并发送一个字符串表示你想添加到命令结尾的参数,最终会有一些奇怪的语法,但只要你意识到它至少会为你节省一大堆打字。 –

+0

本文中的建议可能对您有所帮助:[Windows IT Pro - 在PowerShell中运行可执行文件](http://windowsitpro.com/powershell/running-executables-powershell)。 –

+0

另外,请给出一个例子,你的意思是“我不能像这样把任何参数传递给.jar文件,因为Powershell拦截它们并将它们解释为函数参数。” –

回答

0

我没有plantuml或任何类似与测试,但你可以通过与$args变量函数的所有参数,所以这种方法可能会奏效:

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 
} 
+0

我认为这个答案正在朝着正确的方向发展!但是,运行plantuml -testdot的预期行为是输出在PowerShell终端本身,而现在它似乎为Java进程打开了一个新的终端,之后它立即再次关闭。最大的缺点是我看不到打印结果,因为窗口立即关闭。有任何想法吗? – PostLee

+0

更新:通过-NoNewWindow参数固定,但现在终端不等待plantuml完成并且输出位于奇怪的地方(即,如果我开始写入,我覆盖输出)。我尝试添加-Wait,但似乎没有任何效果。 *编辑*:请忽略上述,-Wait参数突然工作,猜我忘了重新加载我的PowerShell。感谢您的帮助! – PostLee

+0

@postlee - NoNewWindow是一个很好的抓住,我很高兴你有 - 等待工作,否则我不知道还有什么建议:) – TessellatingHeckler