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?
Related
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.
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
I'm trying to test BeanShell's command line interpreter in how it processes basic Java commands and syntax on my machine, and see if I can customise its behavior in any way. I've installed version 2.0b4 on my machine running OS X 10.10.1 (the JAR file is in /Library/Java/Extensions as per the instructions).
It's the closest thing to what I've been looking for, an interactive Java interpreter, but it doesn't have some standard features which a good interpreter should have.
I'd like to be able to use the Up arrow key to reuse a previous command, but at the moment it doesn't recognise it, it just shows a control sequence. Is there a way to customise this for BeanShell?
Is there a way to get BeanShell to print out the value of a variable if I've created it beforehand, just by naming it, like
String s = new String( "Hello World!" );
s;
Hello World!.
This is possible in Python for example.
According to the documentation on importing Java classes which(<java class>); should return the classpath location of the specified Java class. But which( java.lang.String ); does not work for me, I get a NullPointerException:
bsh % which(java.lang.String);
Start ClassPath Mapping
Mapping: Directory /Users/srm
// Error: // Uncaught Exception: Method Invocation cp.getClassSource : at Line: 42 : in file: /bsh/commands/which.bsh : cp .getClassSource ( className )
Called from method: which : at Line: 8 : in file: : which ( java .lang .String )
Target exception: java.lang.NullPointerException
java.lang.NullPointerException
Any pointers or help would be appreciated.
Run beanshell with jline.
Download jline jar from http://jline.sourceforge.net/index.html and then you can do:
java -cp jline-1.0.jar:bsh-2.0b4.jar jline.ConsoleRunner bsh.Interpreter
Line editing capability will be provided by jline. I found this hint here.
There are issues running with jline2. First, you'll get:
$ java -cp jline-2.12.jar:bsh-2.0b4.jar jline.ConsoleRunner bsh.Interpreter
Exception in thread "main" java.lang.NoClassDefFoundError: jline/ConsoleRunner
Due to this issue which is fixed. But then, use the new class and you still get:
$ java -cp jline-2.12.jar:bsh-2.0b4.jar jline.console.internal.ConsoleRunner bsh.Interpreter
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
due to this issue which is not fixed yet.
Use show() command which will trigger showing of value.
bsh % show();
bsh % String s = new String("Hello World");
bsh % s;
<Hello World>
bsh %
It is mentioned in the Useful BeanShell Commands section of the documentation.
Doesn't work for me either
It doesn't fail in my case, but it didn't find it either.
bsh % which(java.lang.String);
Start ClassPath Mapping
Mapping: Archive: file:/Users/me/beanshell/jline-1.0.jar
Mapping: Archive: file:/Users/me/beanshell/bsh-2.0b4.jar
Mapping: Archive: file:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar
End ClassPath Mapping
null
bsh %
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.)
struggling with this for an hour... java code:
ULogger.info("throwing out 666!");
System.exit(666);
bash wrapper:
eval ${COMMAND_TO_RUN}
ret_code=$?
printf "error code : [%d]" ${ret_code}
output:
[2012-11-30 15:20:12,971][INFO ] throwing out 666!
error code : [0]
what's the deal here? Thanks...
[EDIT]
The ${COMMAND_TO_RUN} is
((java -Xmx9000m -Dtoday_nix=20121128 -cp "/usr/lib/hadoop/conf" com.paypal.risk.ars.linking.task_fw.BaseRunnableProcess 3>&1 1>&2 2>&3) | tee /dev/tty) > batches_errors.log
Your problem is in your COMMAND_TO_RUN:
((java -Xmx9000m -Dtoday_nix=20121128 -cp "/usr/lib/hadoop/conf" com.paypal.risk.ars.linking.task_fw.BaseRunnableProcess 3>&1 1>&2 2>&3) | tee /dev/tty) > batches_errors.log
The last program called is tee, which will exit with status 0, overriding the exit
value of java.
You can use $PIPESTATUS, which is an array of return values in the pipe.
For example:
$ cat nonexistantFile | echo ; echo "e: $? p: ${PIPESTATUS[#]}"
Expected output:
e: 0 p: 1 0
cat will fail (exit code 1), echo will succeed (exit code 0). $? will be 0.
${PIPESTATUS[0]} will contain the exit code for cat (1) and ${PIPESTATUS[1]} the one for echo (0).
This is because $? is almost certainly giving you the return code of eval, which is succeeding here. Why are you including the eval call in there? Just call the COMMAND_TO_RUN