Run Java with a specific argument every time

I have a program that allows me to define a java ( /usr/bin/java

) executable , but does not allow me to add specific arguments to the executable.

I want to be able to run Java every time with a specific argument to enable the security manager.

So far, I've tried to add an argument after /usr/bin/java

, so it looks like

java=/usr/bin/java -Djava.security.manager -Djava.security.policy=/home/java.policy

      

It didn't work as the program is probably checking if the file exists. Another way I tried to do it was to make a bash script named java

that contained:

/usr/bin/java -Djava.security.manager -Djava.security.policy=/home/java.policy $*

      

Then I set the java path to /home/java

(Location of my script). It didn't work either. Is there a way I can do this?

Thank.

+3


source to share


2 answers


Put your Java call in a wrapper script java.sh

:

#!/bin/bash

/usr/bin/java -Djava.security.manager -Djava.security.policy=/home/java.policy $@

      

Change the permissions with chmod u+x java.sh

, then call your program with java=./java.sh

(adapt the path for the script as needed).

Notes on executable bit and shebang line



Both the shebang ( #!/bin/bash

) line and execution permission are important here . Without them, the system calls of the family exec*

will not work, because the kernel does not know what to do with the file, or because of an execution failure due to a missing executable bit.

This happens when running directly from the shell ( ./java.sh

) because most shells have some kind of compatibility feature for this case, so they run the script in the shell if it exec*

fails. Execution permission must be set.

The only case where none of them is not necessary - it is to give your argument as a shell script: bash java.sh

.

+1


source


Your second approach will be ok, but your problem will most likely be that your Multicraft application is not finding your script. Moreover, it is your web server environment (Apache?), Which you may need to change the PATH to find your java script wrapper.



0


source







All Articles