System.exit() status code not working when compiled into native app - java

I've created a JavaFX 2 app of which I'm adding a command-line mode (so that it can be run in a batch mode from nightly scripts). For this to be effective, I want to set the exit status to indicate errors. I'm doing this with this code:
if (errorOccurred) {
Platform.exit();
System.exit(exitCode);
}
This works fine when I run it from the IntelliJ, and I see this in the Console window:
Process finished with exit code 255
When I run the jar from the command line it also works:
$ java <snip-lots-of-arguments> cool_app.Main
$ echo $?
255
But after I use javapackager to turn the jar into a native application, it stops working:
$ javapackager -deploy -native -outdir out -outfile "cool_app.app" -srcfiles cool_app.jar -appclass cool_app.Main -name "cool_app" -title "cool_app"
$ open out/cool_app.app
$ echo $?
0
I'm using JDK 8u40 on MacOS 10.10.4.
Is there something I'm missing? Or a bug in javapackager?

Related

Create a script to run multiple java jar in different terminal windows

I wrote an application in java that needs five players and a server.
I need to write a script that executes the jar of the server and of every single player in different terminal windows. How can I do?
I tried a script and worked but the jar opened in the same terminal window than I tried with xterm or konsole with flag --noclose but does not work (warning command: konsole not found)
#! /bin/sh
xterm --hold -e java -jar /Users/Marco\ 1/Documents/ing-sw-2019-Lentini-Marazzi-Marini/out/artifacts/server_jar/adrenalina.jar
for X in $(seq 5)
do
konsole --noclose -e java -jar /Users/Marco\ 1/Documents/ing-sw-2019-Lentini-Marazzi-Marini/out/artifacts/client_jar/adrenalina.jar gui
done
exit;
To run a process in the background from bash, you'll need to add an & to the end of your command, e.g.
java -jar /path/to/jar/my.jar &
Otherwise bash will wait until the command execution terminates.

cumulocity's rasberrypi's "c8y-agent.sh" file not working

I am trying to connect my cumulocity-rpi-agent to cumulocity so that I can have two options either accept or cancel. But this options will be available only when i execute sh c8y-agent-debug.sh this file using sh command. I followed below steps
1) wget http://resources.cumulocity.com/examples/cumulocity-rpi-agent-latest.deb
2) sudo dpkg -i cumulocity-rpi-agent-latest.deb
And:
login as: pi
pi#raspberrypi.mshome.net's password:
...
pi#raspberrypi:~ $ cd /usr/share/
pi#raspberrypi:/usr/share $ cd cu
cumulocity-rpi-agent/ cups/
pi#raspberrypi:/usr/share $ cd cumulocity-rpi-agent/
pi#raspberrypi:/usr/share/cumulocity-rpi-agent $ sh c8y-agent.sh
^C
pi#raspberrypi:/usr/share/cumulocity-rpi-agent $ sudo nano
pi#raspberrypi:/usr/share/cumulocity-rpi-agent
And:
#!/bin/sh
(
while true
do
echo "Running the Cumulocity Linux Agent..."
java -cp 'cfg/*:lib/*' -Dlogback.configurationFile=cfg/logback.xml c8y.lx.age$
sleep 1
done
) 2>&1 | logger
this is c8y-agent-debug.sh script I execute this file using sh c8y-agent-debug.sh or bash c8y-agent-debug.sh but executing the file not event echo statement.
Do i need to set anything related to java. Java is already installed. Is there any additional setting required for that java -cp command. Java is installed on /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/ on this path. This is on linux. javac and java commands are working fine

Running Spigot BuildTools.jar from PowerShell using Git Bash

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

What is needed to create a jailed environment for Scala

I'm developer of a website where programmers can submit bots that compete against each other in a game. I'm trying to add Scala to our list of supported languages, but I'm having trouble here.
The problem is that every bot runs from it's own jail and I can't get scala to run from the jail (Linux system btw) because I get an error saying the executable is not found. Outside of the jail, everything works perfectly. So I'm missing some stuff to put in to the jail. I have all the Java dependency libraries, the whole jvm folder and the whole scala folder.. but there's some other stuff still missing and I'm clueless what it could be.
Here are the important lines of the compile script (which works as it should I think)
#compile
scalac -sourcepath src/ -d bin/ `find src/ -name '*.scala'`
#create runscript
echo "#!/bin/sh" > bin/run_ai
echo "cd / && ./scala -Djava.security.manager -cp bin/ -J-Xss8m -J-Xmx450m '$MAIN'" >> bin/run_ai
run_ai is the script that is called when running the bot from the jail, $MAIN is the main scala file to be ran. As I said, this all works fine outside of the jail.
Here's the script that creates everything that's needed inside of the jail. Here I'm missing some important stuff obviously.
mkdir -p lib64 bin lib/x86_64-linux-gnu usr/lib/x86_64-linux-gnu scala jvm proc
cp /lib64/ld-linux-x86-64.so.2 lib64/
cp /bin/sh bin/
#These dependency libraries are actually copied from the Java mkjail script.
#Using 'ldd /usr/bin/scala' returns "not a dynamic executable" in the shell.
#For other languages, it gives a nice list of dependencies, scala not :(
cp /lib/x86_64-linux-gnu/libgcc_s.so.1 lib/x86_64-linux-gnu/
cp /lib/x86_64-linux-gnu/libdl.so.2 lib/x86_64-linux-gnu/
cp /lib/x86_64-linux-gnu/libz.so.1 lib/x86_64-linux-gnu/
cp /lib/x86_64-linux-gnu/libc.so.6 lib/x86_64-linux-gnu/
cp /lib/x86_64-linux-gnu/libpthread.so.0 lib/x86_64-linux-gnu/
cp /lib/x86_64-linux-gnu/libm.so.6 lib/x86_64-linux-gnu/
cp /usr/lib/x86_64-linux-gnu/libstdc++.so.6 lib/x86_64-linux-gnu/
mount -o bind /proc proc
echo "bound /proc"
mount -o remount,ro proc
echo "remounted /proc"
#adding jvm to jail
mount -o bind /usr/lib/jvm jvm
mount -o remount,ro jvm
#adding scala to jail
mount -o bind /usr/share/scala scala
mount -o remount,ro scala
#some extra stuff to run Java from the jail, not actually needed here I think
if [ -f jvm/java-7-openjdk-amd64/jre/bin/java ]
then
cp jvm/java-7-openjdk-amd64/jre/lib/amd64/jli/libjli.so lib/x86_64-linux-gnu/
ln -s jvm/java-7-openjdk-amd64/jre/bin/java .
else
cp jvm/java-6-openjdk-amd64/jre/lib/amd64/jli/libjli.so lib/x86_64-linux-gnu/
ln -s jvm/java-6-openjdk-amd64/jre/bin/java .
fi
#creating a soft link to run scala. trying to run the bots without the soft link gives the same error, so this is not the problem.
ln -s scala/bin/scala ./scala
So in short: running scala from the jail results in an error: "./scala not found", if I try other stuff like: /scala/bin/scalac, I get the same error. (running the Java that is added in the jail works fine). So I'm missing some stuff to run Scala.
My question is: What is exactly needed to run Scala?
The 'scala' executable references /usr/bin/env, which resides outside the jail.
This might work:
java -cp /absolute/path/to/scala-library.jar:path/to/scala/classfiles/ MyMainClass
scala and scalac are shell scripts. They might be missing the env and bash executables.
You can further debug the scripts by adding trace code (e.g. echo "trace01") to pinpoint a problem.

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