i am trying to trigger an email alert based on the java script output but i am getting an error like below in the shell script,
script.sh: line 22: [: : integer expression expected
Below is my shell script format,
out="$(java -jar /waitrose/scripts/OF/BOBIErrorAutomation/BOBIAutomation.jar
2>&1)"
if [ "$out" -gt 0 ]
then
mail -s "script did not completed successfully" $mailid_list
exit 0
fi
Below is the sample error returned by the java program :
The error was: com.ibm.db2.jcc.c.a.<init>(a.java:174) 174
com.ibm.db2.jcc.c.b.a(b.java:1745) 1745
com.ibm.db2.jcc.b.p.<init>(p.java:934) 934
Can anyone tell me how to capture the output from the java program and trigger the mail in shell ?
are you not comparing the output of your java program which in the case where it crashes, most likely going to be a string of some sort, against (-gt greater than) an integer? the comparator expects a whole number, not a crash message from java.
EDIT: ill elaborate, perhaps it would be better to test if $out is an integer first, if your jar only outputs integers if it runs correctly:
if ! [[ "$out" =~ ^[0-9]+$ ]]
then use this to trigger your mail, and else to normal functionality.
Related
I am trying to install java using powershell Invoke-WebRequest command on packer instance however, I am getting below error;
Cannot index into a null array.
At line:1 char:94
+ ... content | %{[regex]::matches($_, '(?:<a title="Download Java software ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Command;
[Net.ServicePointManager]::SecurityProtocol = "tls12"
$URL = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content | %{[regex]::matches($_, '(?:<a title="Download Java software for Windows .64-bit." href=")(.*)(?:">)').Groups[1].Value}
Invoke-WebRequest -UseBasicParsing -OutFile jre8.exe $URL
Start-Process .\jre8.exe '/s REBOOT=0 SPONSORS=0 AUTO_UPDATE=0' -wait
Few weeks ago I was able to run it successfully but since yesterday getting the above error.
Any advise?
Thanks.
This happens, as there is no such as string as Download Java software for Windows on the web page. Since the regex doesn't match anything, Groups member doesn't exist and you'll get an error about trying to index into a non-existing member.
Either use a web browser's View Source command, or save the content on a text file and view it with Notepad like so,
$cc = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content
Set-Content -Path c:\temp\javapage.txt -Value $cc
notepad c:\temp\javapage.txt
The page loads a bunch of Javascript that generates the actual page seen on a browser.
I have some issues with getting the java version out as a string.
In a batch script I have done it like this:
for /f tokens^=2-5^ delims^=.-_^" %%j in ('%EXTRACTPATH%\Java\jdk_extract\bin\java -fullversion 2^>^&1') do set "JAVAVER=%%j.%%k.%%l_%%m"
The output is: 1.8.0_121
Now I want to do this for PowerShell, but my output is: 1.8.0_12, I miss one "1" in the end Now I have tried it with trim and split but nothing gives me the right output can someone help me out?
This is what I've got so var with PowerShell
$javaVersion = (& $extractPath\Java\jdk_extract\bin\java.exe -fullversion 2>&1)
$javaVersion = "$javaVersion".Trim("java full version """).TrimEnd("-b13")
The full output is: java full version "1.8.0_121-b13"
TrimEnd() works a little different, than you might expect:
'1.8.0_191-b12'.TrimEnd('-b12')
results in: 1.8.0_19 and so does:
'1.8.0_191-b12'.TrimEnd('1-b2')
The reason is, that TrimEnd() removes a trailing set of characters, not a substring. So .TrimEnd('-b12') means: remove all occurrences of any character of the set '-b12' from the end of the string. And that includes the last '1' before the '-'.
A better solution in your case would be -replace:
'java full version "1.8.0_191-b12"' -replace 'java full version "(.+)-b\d+"','$1'
Use a regular expression for matching and extracting the version number:
$javaVersion = if (& java -fullversion 2>&1) -match '\d+\.\d+\.\d+_\d+') {
$matches[0]
}
or
$javaVersion = (& java -fullversion 2>&1 | Select-String '\d+\.\d+\.\d+_\d+').Matches[0].Groups[0].Value
I am encountering a problem of calling an external program (especially, a SAT solver Picosat) from a Scala program.
Specifically, the problem is that the system returns "java.lang.RuntimeException" with "Nonzero exit value: -1073741515" when calling Picosat from ScalaIDE, although the system works when calling it from a (Cygwin) terminal.
As an example, a minimal code example of this problem is follows:
import sys.process._
object Hello {
def main(args: Array[String]): Unit = {
val result = "picosat -h".!!
println(result)
}
}
The executing this sample code returns the following exception message and just exits, when executing the sample code from ScalaIDE:
Exception in thread "main" java.lang.RuntimeException: Nonzero exit value: -1073741515
at scala.sys.package$.error(package.scala:27)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.slurp(ProcessBuilderImpl.scala:132)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang$bang(ProcessBuilderImpl.scala:102)
at Hello$.main(Hello.scala:5)
at Hello.main(Hello.scala)
although it prints correct "help message" of Picosat when executing the sample code from Cygwin as follows:
> scala src/Hello.scala
usage: picosat [ <option> ... ] [ <input> ]
where <option> is one of the following
-h print this command line option summary and exit
--version print version and exit
--config print build configuration and exit
-v enable verbose output
-f ignore invalid header
-n do not print satisfying assignment
-p print formula in DIMACS format and exit
--plain disable preprocessing (failed literal probing)
-a <lit> start with an assumption
...
I am grateful if you could provide any clues for this problem?
So I have the following command that runs java application from the command line and produces a bunch of output.
java -jar client.jar --server test7.contoso.com --alternativeemailaddress fred1#contoso.com --organisation contoso --emailaddress fred#contoso.com --data1 1234 --Password1 123456 --data2 1234 --Password2 1234
I'm trying to run this from within PowerShell and capture the output to a variable for further processing.
So far I've found 100's of different ways to do this, but none have really worked particularly well.
Is there any best practice/convention as to how to get this done?
See: https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters/
For many ways to execute "legacy" commands with PowerShell
You need to include the .exe for one.
The new V3+ recommended way is to use --% to tell PowerShell to not parse the remaining arguments
PS> java.exe --% -jar client.jar --server test7.contoso.com --alternativeemailaddress fred1#contoso.com --organisation contoso --emailaddress fred#contoso.com --data1 1234 --Password1 123456 --data2 1234 --Password2 1234
OK so I worked it out. This is what I went with.
$params = #{ 'jar'='C:\client\client.jar';
'param1'='data1'
'param2'='data2'
'param3'='data3'
'param4'='data4'
'param5'='data5'
'param6'='data6'
'param7'='data7'
'param8'='data8'
'param9'='data9'
'param10'='data10'
'error_log'='C:\tmp\error_log.txt'
}
$data = & 'C:\Program Files\java\jdk1.8.0_121\bin\java.exe' -jar $params.'jar' `
--param1 $params.param1 `
--param2 $params.param2 `
--param3 $params.param3 `
--param4 $params.param4 `
--param5 $params.param5 `
--param6 $params.param6 `
--param7 $params.param7 `
--param8 $params.param8 `
--param9 $params.param9 `
--param10 $params.param10 `
2>$params.error_log
As a JAVA teaching assistant, I get stuck with grading a lot of student's labs. A lot of these labs use a Scanner to get input from a user. Instead of repeated bashing numbers into the keyboard, is there a way I can utilize a heredoc to run all of the labs with the same input parameters without changing the student's code? What I have so far (which works for except the heredoc-esque code):
#!/bin/bash
for i in unzip/*; do
echo $i
javac $i/lab0/AddThree.java
cd $i/lab0
java AddThree <<EOF
2
3
4
EOF
cd ../../..
done
The code I'm trying to grade adds three integers that are provided by the user. unzip is the directory where each student has a folder (i.e. file structure is ./unzip/student/lab0/sourcecode.java)
Java gives:
unzip/student
Hello out there
I will add three numbers for you
Enter three whole numbers on a line :
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at AddThree.main(AddThree.java:10)
./lab0test: line 9: 2: command not found
./lab0test: line 10: 3: command not found
./lab0test: line 11: 4: command not found
./lab0test: line 12: EOF: command not found
Your heredoc looks good except it will send the spaces at the start of each line. To get rid of those, you can either use -EOF and start each line of the heredoc with Tab characters, which will get stripped:
cd $i/lab0
java AddThree <<-EOF
TabTab2
TabTab3
TabTab4
TabTabEOF
cd ../../..
Or unindent the heredoc contents. It's ugly, but it'll work.
cd $i/lab0
java AddThree <<EOF
2
3
4
EOF
cd ../../..
Alternatively, if the input is short enough you could do it inline:
java AddThree <<< $'2\n3\n4'
(Using $'...' tells the shell to interpret \n escape sequences.)