Running Spigot BuildTools.jar from PowerShell using Git Bash - java

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"'

Related

installation of java using powershell does not work

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

not able to execute next statement in a shell script after running a java jar app using nohup

Not able to execute other instruction after if I am running a java jar file (which is present on another host) from my shell script.I tried using nohup but still not able to exit.
following is my script
#!/usr/bin/env bash
sshpass -p "${array[1]}" ssh -oStrictHostKeyChecking=no ${array[0]}#${array[2]} "cd ${array[3]} && echo -ne '\n' | nohup java -jar myapp.jar";
#some other instructions
echo "next statement"
already tried Scripts with nohup inside don't exit correctly but it doesn't worked.
sshpass -p pass ssh -o StrictHostKeyChecking=no xyz#hostname 'cd dir; nohup java -jar myapp.jar'
Try this with replacing the values appropriately

powershell invoke-expression java program get status

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.

How to call a Java program from PowerShell?

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

Jar not running

So I created a script the the following commands
#! /usr/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
JAVA=/usr/bin/java
MY_SERVER=/home/user/Desktop/Hello.jar
USER=user
/bin/su - $USER -c "$JAVA -jar $MY_SERVER &"
And I saved it in
etc/init.d/
And then ran the following command in terminal
sudo update-rc.d java_server_launch.sh defaults
I have a program located at
/home/user/Desktop/
And it is called Hello.jar and it works fine when I run it. When I restart my computer for some reason the program (Hello.jar) does not execute. What am I doing wrong?
I'm doing exactly what the answer here says.
You need to replace Hello.jar with $MY_SERVER in the last line of your bash script. That's because your current working directory isn't /home/user/Desktop
Edit: Try replacing the last line of code with this:
/bin/su $USER -c "$JAVA -jar $MY_SERVER &"
if you're running on Ubuntu you should check out upstart
see how simple it is to run jar https://stackoverflow.com/a/12102542/41576

Categories

Resources