How to launch Java with options -X from a shell script - java

I want to create a script (foo.sh) such as:
#!/bin/sh
set -o posix
. setpath.sh
java foo.Bar "$#"
and I want to use like:
./foo.sh -p -o -ff filename
but the shell returns an error message. illegal option
if I launch
java foo.Bar -p -o -ff filename
then it works.
How can create a script for that?
I guess
. setpath.sh
has problem..
If I change it to
. setpath.sh --
then
./foo.sh -anyoptoptions
works...
but I have quite no idea what happens...

Related

How can I do Java/Kotlin to CLI?

I have a Java/Kotlin program, which gets arguments from String[] args, and I need to make it executable from everywhere from console, without prefixing it with java word. Like only the name of the program and its arguments. How can I do it?
Like git or heroku:
name command
It depends on your operating system, but on Unix the following script would work:
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar ${MYSELF}.jar "$#"
exit 1
You need to append this script at the start of your jar using cat, like the following
cat script.sh my.jar > my-program
And move my-program to some dir in your $PATH. After that, you'll be able to call my-program as usual program.

Docker image openjdk:8-jdk-alpine fails to execute a simple command

I created a docker image from openjdk:8-jdk-alpine using the below Dockerfile:
But when I try to execute simple commands I get the following errors:
/bin/sh: ./run.sh: not found
my run.sh looks like this
enter image description here
I try to "docker run -it [images] bash" enter to the interactive environment,I can see the file "run.sh".In the directory /bin bash exist,but I execute run.sh also display " /bin/sh: ./run.sh: not found"
PS:Sorry for my poor english,I am a chinese student
The printed content of the run.sh, indicates that my original assessment was incorrect; however based on the error message, and the image of the run.sh file, I have a lead.
Your run.sh script has an exec line of #!/bin/sh, which means that it does not need bash to operate so my previous assessment was incorrect.
Starting on a mac, I created a run.sh script, duplicated the dockerfile (mostly), and it ran correctly, producing a valid run.
I then converted the run.sh to use dos line endings and got the following:
$ file run.sh
run.sh: POSIX shell script text executable, ASCII text, with CRLF line terminators
$ docker run --rm -it bob
/bin/sh: ./run.sh: not found
Which looks suspiciously like your error message.
From this, it would lead me to believe that your run.sh file contains dos line endings. Based on the images, I'm guessing that you're on windows, which is where the problem with the run.sh script originates.
how to convert the line endings (some examples):
dos2unix run.sh
perl -pi -e 's/\r\n/\n/g' run.sh
Previous Answer
The most likely reason for this issue is that the shebang line in the run.sh contains: #!/usr/bin/bash, or something of that ilk - i.e. it doesn't reference the valid path to the binary that will run the shell script.
On alpine, bash is installed into /bin, so if you try to run the script you will see the error:
/ # ./run.sh
/bin/sh: ./run.sh: not found
/ # cat run.sh
#!/usr/bin/bash
echo "Hi"
workaround (1): after the apk add bash, do an:
RUN ln -s /bin/bash /usr/bin
in your Dockerfile. This will create a symlink for bash, allowing the program to run:
/ # ln -s /bin/bash /usr/bin
/ # ./run.sh
Hi
workaround(2) - if you don't want to make a symlink like this, you can always invoke bash as part of the CMD -
CMD [ 'bash', './run.sh' ]

How can I run the attribute selection WEKA library from command line in a python file?

I follow the below structure to convert a csv file into an arff file, and it works. But for running the attribute selection library from WEKA it does not work. Does anyone know?
java -cp ~/weka.jar weka.attributeSelection.CfsSubsetEval 1 -E 1 "weka.attributeSelection.BestFirst -D 1 -N 5" -i ~/file.arff
I also tried the flooring command:
java -cp ~/somewher/weka.jar weka.attributeSelection.InfoGainAttributeEval "weka.attributeSelection.Ranker -T -1.7976931348623157E308 -N -1" last -c -I ~/somewher/file.arff
But it gives me this error:
java.lang.Exception:
No training file given.
General options:
-h display this help
-i <name of input file>
Sets training file.
-c <class index>
Sets the class index for supervised attribute
selection. Default=last column.
I'm aware that I'm answering an old question, but the correct way to call Weka to select features is like this:
java weka.filters.supervised.attribute.AttributeSelection -E weka.attributeSelection.CfsSubsetEval -S "weka.attributeSelection.BestFirst -D 1 -N 5" -i input.arff -o output.arff
(also this is not related to Python)

-I flag JAVA command # .bat CMAKE

I have to extract and generate some .h files from a jar and idl file.
Need to use a .bat from external developers:
#echo off
java -jar C:\WorkspaceSwim\trunk\iB\build\BlueGen\target\bluegen.jar -o C:\WorkspaceSwim\trunk\Example\Distribution\build\GeneratedDDSFiles C:\WorkspaceSwim\trunk\Example\Distribution\Flight.idl
set NDDSHOME=C:\WorkspaceSwim\trunk\iB\build\ToolBox\NDDS
set RPCDDSHOME=C:\WorkspaceSwim\trunk\iB\build\ToolBox\RPCDDS
setlocal
set dir=%~dp0
set args=%1
:getarg
shift
if "%~1"=="" goto continue
set args=%args% %1
goto getarg
:continue
set JREHOME=%JAVA_HOME%
"%JREHOME%\bin\java" -DPATH="%PATH%" -DNDDSHOME="%NDDSHOME%" -DRPCDDSHOME="%RPCDDSHOME%" -Djava.ext.dirs="%RPCDDSHOME%\classes\" com.eprosima.rpcdds.idl.RPCDDSGEN %args% "-I%RPCDDSHOME% /idl"
I think there are some fail on the script , but i can't see it. Maybe a problem with quotes? i tryied this 3 posibilities :
"%JREHOME%\bin\java" -DPATH="%PATH%" -DNDDSHOME="%NDDSHOME%" -DRPCDDSHOME="%RPCDDSHOME%" -Djava.ext.dirs="%RPCDDSHOME%\classes\" com.eprosima.rpcdds.idl.RPCDDSGEN %args% "-I%RPCDDSHOME%\idl"
Error: principal class \idl doesn't found or load
"%JREHOME%\bin\java" -DPATH="%PATH%" -DNDDSHOME="%NDDSHOME%" -DRPCDDSHOME="%RPCDDSHOME%" -Djava.ext.dirs="%RPCDDSHOME%\classes" com.eprosima.rpcdds.idl.RPCDDSGEN %args% "-I%RPCDDSHOME%\idl"
Error: principal class com.eprosima.rpcdds.idl.RPCDDSGEN doesn't found or load
"%JREHOME%\bin\java" -DPATH="%PATH%" -DNDDSHOME="%NDDSHOME%" -DRPCDDSHOME="%RPCDDSHOME%" -Djava.ext.dirs="%RPCDDSHOME%\classes\ com.eprosima.rpcdds.idl.RPCDDSGEN %args%" -I "%RPCDDSHOME%\idl"
Unrecognized option: -I
Error: Could not create the Java Virtual Machine.
Previusly i use this .cmake to generate the code that will run this .bat
add_custom_command(
OUTPUT ${DDSGeneratedFiles}
COMMAND java -jar ${iB_BlueGen} -o ${GeneratedDDSDirName} ${LocatedFile}
COMMAND "set NDDSHOME=${iMASBlue_NDDS_DIR} &" "set RPCDDSHOME=${iB_RPCDDS_DIR} &" ${RPCDDSGenerator} -replace -d ${GeneratedDDSDirName} -I${CMAKE_CURRENT_SOURCE_DIR} ${LocatedFile}
DEPENDS ${IncludeDependencies}
MAIN_DEPENDENCY ${LocatedFile}
COMMENT "Calling RPCDDS Generator '${RPCDDSGenerator}' on '${IdlFile}'"
)
Thnx for your answers.
To debug batch files, change line
#echo off
in your batch file to
#echo on
and run again

How to run a script in user mode inside an executable jar that is executed as root?

I want to run the following script within a Java executable jar on the Raspberry Pi.
the script (= stream.sh):
#!/bin/sh
raspivid -fps 25 -w 640 -h 360 -vf -n -o - -t 999999 |cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/cam.sdp,rtcp-mux}' :demux=h264
the Java code:
Runtime.getRuntime().exec("sh stream.sh"));
The problem is that the jar must be run with sudo and the vlc command doesn't accept sudo. Neither the script or the Java code contain sudo but as the jar is executed as sudo, vlc still gives the error "VLC is not supposed to be run as root...".
What is the easiest way to make the script run in user mode inside the jar?
I would use su -l $LOGIN -c $CMD or sudo -u $LOGIN $CMD.
Runtime.getRuntime().exec("sudo -u myuser sh stream.sh"));
man sudoers has all the information you need.
You need to change /etc/sudoers

Categories