I need to call a java program (jar file )from PowerShell.
The following code works:
java -jar $cls --js $dcn --js_output_file $dco
But I need to have to run the app in a process (using Start-Process).
I am trying the following with no sucess:
Start-Process -FilePath java -jar $cls --js $dcn --js_output_file $dco -wait -windowstyle Normal
Error:
Start-Process : A parameter cannot be found that matches parameter name 'jar'.
Any idea how to fix it?
You will need to use following format for powershell:
Start-Process java -ArgumentList '-jar', 'MyProgram.jar' `
-RedirectStandardOutput '.\console.out' -RedirectStandardError '.\console.err'
Or other option you can use is Start-job:
Start-Job -ScriptBlock {
& java -jar MyProgram.jar >console.out 2>console.err
}
It looks like the -jar is being picked up as an argument of Start-Process rather than being passed through to java.
Although the documentation states that -ArgumentList is optional, I suspect that doesn't count for -option-type things.
You probably need to use:
Start-Process -FilePath java -ArgumentList ...
For example, in Powershell ISE, the following line brings up the Java help (albeit quickly disappearing):
Start-Process -FilePath java -argumentlist -help
but this line:
Start-Process -FilePath java -help
causes Powershell itself to complain about the -help.
Option 1 [Using Start-Job ScriptBlock]
Start-Job -ScriptBlock {
& java -cp .\Runner.jar com.abc.bcd.Runner.java >console.out 2>console.err
}
if ( $? == "True")
write-host("Agent started successfully")
else if ($? == "False")
write-host("Agent did not start")
Option 2 [Using Start-Process]
Start-Process -FilePath '.\jre\bin\java' -WindowStyle Hidden -Wait -ArgumentList "-cp .\Runner.jar com.abc.bcd.Runner"
That's how i did it using above two options initially.
Option 3 [Using apache-commons-daemon]
I can suggest a better and robust alternative.
You can use apache-commons-daemon library to build a windows service for your java application and then start, stop the service very conveniently.
There is amazing youtube video which will explain apache commons daemon and how to build a windows service. I will attach the reference at the end.
References :
https://commons.apache.org/proper/commons-daemon/index.html
https://www.youtube.com/watch?v=7NjdTlriM1g
Related
I use the following command in powershell to install java using the jdk that I have
start-process -filepath jdk-x64.exe -passthru -wait -argumentlist "/s,INSTALLDIR=c:\progra~1\jre,/L,myinstallogs.log"
It just starts a process and never install java in program files
When I run PS command, all i see is a new process started but not the actual installation.
Not sure what am I doing wrong
I'm running a jar from powershell using Invoke-Expression:
$line="c:\temp\my file.txt"
$cmd="java -jar C:\temp\post.jar `"" + $line + "`""
invoke-expression $cmd
If I test $? it will only give me the status of Invoke-Expression which will always be a success as the java executable and post.jar can be found.
Also, I've thought of running the java program directly and test $? but sometimes the file name can be very complex and sometimes I put extra options.
How do I get the status of the run of the post.jar program?
Thanks.
You could try to check the exit code of the process via:
$p = Start-Process java -ArgumentList '-jar C:\temp\post.jar',$file -Wait -PassThru
$p.ExitCode
Based on -PassThru you'll receive a System.Diagnostics.Process-object that should include the exit-code of the jar. For more info see the Start-Process cmdlet description.
Hope that helps.
I'm trying to write a PowerShell script to update Spigot using Git Bash. Hopefully from the two failed PS examples below you get the gist of what I'm trying to do.
I can successfully open a Git Bash shell in the target folder and run java -jar BuildTools.jar. When I try to run through PowerShell, a CMD window opens and immediately closes. No errors are displayed and best I can tell, the CMD window contains no text. I prefer to use PowerShell over a CMD script because I am leveraging Invoke-WebRequest earlier on to get the latest version of BuildTools.jar. I would like to keep all this together in one script.
Example 1:
Start-Process -FilePath "C:\Program Files\Git\bin\bash.exe" -ArgumentList "--login -i -c ""java -jar BuildTools.jar"""
Example 2:
Start-Process -FilePath "C:\Program Files\Git\bin\bash.exe" -ArgumentList '--login', '-i', '-c', '"java -jar BuildTools.jar"'
Figured it out thanks to an idea from Ansgar Wiechers.
Solution is as follows:
Start-Process -FilePath "C:\Program Files\Git\bin\bash.exe" -ArgumentList '-c', '"cd /c/Bitnami/Updater && java -jar BuildTools.jar"'
I have a powershell script that is calling a jar via the following code:
Start-Process java -ArgumentList '-jar', "$jarPath", "$csvPath"
However, the output from the jar is not coming through. I'm pretty sure its running successfully, but I'd like to be sure. How can I pass it through to the Powershell console?
Replace Start-Process with the call operator:
& java -jar $jarPath $csvPath
This works fine for me:
$stdout = "C:\temp\stdout.txt"
Start-Process powershell -ArgumentList "echo 123" -RedirectStandardOutput $stdout -Wait
$output = Get-Content $stdout
echo $output
Remove-Item $stdout
Since I started Powershell process with command echo 123, it returned 123to stdout, so this value is saved to file.
Swap Powershell with Java and it should be working as you expect.
Remember, that you cannot redirect stdout directly to variable, you must do it via file.
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
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).
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
Another example based on https://gaming.stackexchange.com/questions/24543/how-do-i-change-player-name-in-minecraft-multiplayer-in-offline-mode-in-linux
function mineCraftAs {
Param (
[parameter(mandatory=$true, HelpMessage="Minecraft character name." ,ValueFromPipeline=$true)]
[string] $name
)
if(!(test-path $env:appdata)) { $(throw "Appdata not found at $env:appdata")}
$private:minecraftPath=Join-Path $env:appdata .minecraft
if(!(test-path $minecraftPath)) { $(throw "Minecraft not found at $minecraftpath")}
$private:minebinPath=join-path $minecraftPath "bin"
if(!(test-path $minebinPath)) { $(throw "Minecraft bin not found at $minebinPath")}
$minebinPath | write-debug
gci $minebinpath | write-debug
#java -Xms512m -Xmx1024m -cp "%APPDATA%/.minecraft\bin\*" -Djava.library.path="%APPDATA%\.minecraft\bin\natives" net.minecraft.client.Minecraft '"'%1'"'
echo java -Xms512m -Xmx1024m -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)
$minecraftJob=& 'C:\Program Files (x86)\Java\jre6\bin\java.exe' -Xms512m -Xmx1024m -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)
}
minecraftas newbie
The following should work:
java.exe -cp '.;./common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install
I guess that PowerShell interprets the ; in the classpath as command delimiter, thereby trying to run java -cp . and ./common.jar -D....
start-process -nnw java "-cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install"