简体   繁体   English

如何使用Powershell调用Java exe文件?

[英]How to call a java exe file using powershell?

I have created an application window using Java swing. 我已经使用Java swing创建了一个应用程序窗口。 I need to call my java exe file using powershell.. The Java application is done using java swing..I have exported the Java application to a jar file.. While double clicking the exe file i get the output executed.. My powershell code is this 我需要使用powershell调用我的java exe文件。Java应用程序是使用java swing完成的。我已将Java应用程序导出到jar文件中。在双击exe文件的同时,我执行了输出。这是

$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.CreateNoWindow = $true $psi.UseShellExecute = $false
 $psi.FileName = 'java.exe' $psi.Arguments =
 @("-classpath","$Env:CLASSPATH","D:\")
[System.Diagnostics.Process]::Start($psi) set-item -path Env:CLASSPATH
-value :\D:\ "CLASSPATH = $Env:CLASSPATH"  [Diagnostics.Process]::Start('java.exe','-classpath $Env:CLASSPATH
 D:\Swing.jar')

I am geeting a black window displayed and no errors.. My application program window is not displayed 我正在查看显示的黑色窗口,没有错误。.我的应用程序窗口未显示

Two things: 两件事情:

  1. You need the -jar option to Java, as Sanjeev already noted. 正如Sanjeev已经指出的那样,您需要Java的-jar选项。

  2. Please don't write PowerShell as if it were some kind of C# with funky syntax. 请不要编写PowerShell,就好像它是某种带有时髦语法的C#。 There is a Start-Process cmdlet, after all, if you really need it: 毕竟,如果确实需要,则有一个Start-Process cmdlet:

     Start-Process -FilePath java.exe -ArgumentList -jar,D:\\Swing.jar,-classpath,$Env:CLASSPATH -NoNewWindow 

    In this case you probably don't, so the following should work just as well (remember, PowerShell is a shell, hence the name; running programs is quite easy): 在这种情况下,您可能不会这样做,因此以下内容也应同样有效(请记住,PowerShell是一个外壳,因此得名;运行程序非常容易):

     java -jar D:\\Swing.jar -classpath $Env:CLASSPATH 

And if you don't want to have the console output, then use javaw instead of java . 而且,如果您不想获得控制台输出,请使用javaw而不是java Control will return immediately to PowerShell, then, though. 但是,控制权将立即返回到PowerShell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM