I have written a Java program that takes in arguments and then executes. I pass in these arguments from the command line (I am on a Macbook Pro using Terminal, using the bash shell). Let's say the name of my program is prgm. I want to be able to say "prgm " from any directory in the shell and then have that program execute. So I figure I need to write a bash script to reference the Java files and take in arguments, and put that bash script somewhere in my PATH. Where do I put the bash file, and where do I put my Java files? Also, am I right to assume that I only need the .class (binary) Java files?
Step-by-step:
Assuming that the name of the Java executable if myjavaprog.
Assuming that the name of your bash script is myscript.
Make sure myscript is calling myjavaprog using absolute path and the desired arguments.
call echo $PATH and you will see a bunch of paths: /some/path1:/some/other/path2:...
Put your bash script in whatever path you want from the ones returned by echo $PATH.
Go to a random path.
Call you bash script bash myscript. See the execution of myjavaprog.
Tips:
If java program is for personal use only, put it in a path starting with /usr/ or even in your $HOME directory (and add that location to your PATH)
If java program must be shared with other users, put it in an accessible place, so that other users don't need to modify their PATH variable.
Related
Everybody probably has used install .exe files. But how to make them and when does it make sense to make one?
For example, I would have had programmed commercial software in Python, c++, etc. with different files a GUI and pictures and all of the other stuff.
When I want to deliver my product to my customer I don't want to give them a folder and say you need to install Python or Java and execute the program via your command line.
How can I create an executable file that installs the required language and sets up local instances and arranges all files into the correct order?
To create an executable file from a python program, there is pyinstaller. I don't know about java at the moment. The command is as follows :
pyinstaller fileName.py
You can add args (and there are 2 really helpful ones) :
pyinstaller --onefile -w fileName.py
--onefile will put everything into one single file (recommended) and -w will prevent the console from opening when running the .exe file. Add it if you're running a GUI or something. If you need to console for input, don't add -w.
If you want to automate a command line in cmd, create a shortcut leading to C:\Windows\system32\cmd.exe and add /k and your command. For example :
C:\Windows\system32\cmd.exe /k ipconfig
Double clicking on the shortcut will now run the cmd and execute ipconfig automatically. If you want more than one command, you can do command_1 && command_2 && ... && command_n
There is a setup program built into Windows.
Type
iexpress
and follow the wizard.
You need to provide code to run when it finishes to actually install the extracted files.;
I have a program which allows me to define the java executable (/usr/bin/java), but does not allow me to add specific arguments to the executable.
I want to be able to run Java with a specific argument each time in order to enable Security Manager.
So far, I have tried to add the argument after /usr/bin/java, so it looks like
java=/usr/bin/java -Djava.security.manager -Djava.security.policy=/home/java.policy
That did not work as the program probably checks to see if a file exists. Another way I tried was to make a bash script called java which contained:
/usr/bin/java -Djava.security.manager -Djava.security.policy=/home/java.policy $*
I then set the java path to /home/java (Location of my script). That however did not work either. Is there some sort of way I can do this?
Thanks.
Put your java call in a shell script java.sh:
#!/bin/bash
/usr/bin/java -Djava.security.manager -Djava.security.policy=/home/java.policy $#
Change permissions with chmod u+x java.sh, then call your program with java=./java.sh (adapt path for your script as needed).
Notes about executable bit and shebang line
Both the shebang line (#!/bin/bash) and execute permission are important here. Without them, system calls of the exec* family will fail because the kernel does not know what to do with the file or because execution is rejected due to the missing executable bit.
This is different when run directly from a shell (./java.sh), because most shells have some compatibility feature for that case so they will run a script in a shell if exec* fails. The execute permission must be set though.
The only case where neither is needed is if you give your script as argument to the shell: bash java.sh.
Your second approach would be ok, but your problem is likely to be that your Multicraft application is not finding your script. More so, it's the environment of your web server (Apache?) that may need to have PATH altered to be able to find your java wrapper script.
I have created one java program on my Linux system which indents and formats the given file. I want to make that program work like a command in Linux which will take file names and other options as arguments and then will produce the output. I can do this with a C program by just copying the compiled executable in /bin folder but I don't know how to do it with java.
Sample script that can might further help-
#!/bin/bash
#Set whatever number of arguments you expect for the Java jar you have
ARGS_EXPECTED=3
if [ $# -ne $ARGS_EXPECTED ]
then
echo "[$HOSTNAME]: Usage: `basename $0` filename arg1 arg2"
exit 1
fi
java -cp yourfile.jar com.yourpkg.Driver $1 $2 $3
Save the above content to a file, say test.sh
and use the command to give an executable permission chmod +x test.sh
run like ./test.sh filename arg1 arg2 from current directory where test.sh is
I thing this can be useful for your case: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/binfmt_misc.txt?id=HEAD
You can simply run a Java class file or jar file with "java" command from command line. Example:
java -jar yourprogram.jar argument1 argument2
If you save this line in a text file saved for example as "script.sh" and then give it the permission for execution you can run it double clicking or from terminal typing ./script.sh in the same folder containing the file script.sh.
You can also produce scripts that use arguments with $1 $2 etc. avoiding the need of editing file.
http://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-command-line-parameter-to-shell-script-254396/
You can use named parameters, too.
You can also produce a C program for a new command like you suggested that run the "java" command. In this case you can introduce arguments directly from terminal and pass them to java command in the C source.
As others have pointed out it is probably best to use a small shell script to run the Java application. There are several open source products that will help you wrap your Java code to produce a runnable (set of) .jar(s).
If you have correctly separated your business logic from your interface (as you should) then it is probably best if your Java application parses the parameters given on the command line interface. To do this create a separate class for parsing such parameters and calling the classes making up the business logic. Of course this will lead quickly - if not immediately - in writing a parser for Linux like CLI parameters. When this happens you may wish to consider the Apache Commons CLI project.
If you don't want to use any wrapping application/runtime, my method is generally pointing to all the class file containers in the classpath and directly pointing to the class containing the static main method:
java -cp "path_to_jar;path_to_class_folder;etc" "nl.owlstead.stackoverflow.LinuxMain"
I have a Java program utility that I want to execute as a command in cmd. I added the location to the PATH variable, but java programs needs to be executed using java - jar "...". How do I shorten that to just the program name, like mysql or netstat?
Update:
I neglected to mention that this java program takes arguments of its own to handle its tasks, so the batch program would need to pass the arguments passed to it over the the java program. I'm not skilled enough in batch to know how to do this.
~Jacob
You could create a batch file or bash script (depending upon your OS) that calls the program with the proper java -jar commands, and simply name the batch (or bash) script whatever you would like to enter as the command. Place this in a directory that is in your PATH variable, and have at it.
Edit: Read this for info on how to parse command line parameters in batch scripts. Just take the parameters passed to the batch file, parse them, and pass them to your jar file with:
java -jar jarfile.jar param1 param2 ...
So for example, lets's assume that your program takes two arguments. Your script could then be as follows:
java -jar jarfile.jar %1 %2
I am not an expert in batch files by any means, so there is probably a more proper way to do this. That being said, why over complicate things?
With Launch4J you can wrap a Java program in a standalone executable file. I'm not going to copy their (long) feature list here, but definite highlights are the numerous ways presented to customize the resulting exe, its small size, the fact that it's open source and its permissive license that allows commercial usage.
I was trying to execute my java program through shell script so I wrote:
java -jar $(pwd)"/test.jar"
It worked flawlessly but when I turned to the code below:
PATH=$(pwd)"/test.jar"'
java -jar $PATH
Then I got an error: "Run.sh: 3: java: not found"
(Running on Ubuntu)
I have very little experience in shell script so please let me know what's wrong with it. Thanks.
PATH is a special environment variable which the shell uses to find executables. You've changed PATH to point at test.jar, so now the shell can't find java.
Call your variable something else.
Example:
LIB_PATH="$(pwd)/test.jar"
java -jar ${LIB_PATH}
The value in $(PWD) depends on the directory the script is called from (print working directory). If you call the script from another directory, than the one your jar-files resides in, you'll get the wrong path. And you changed the search path of the SHELL, that will prevent the shell from finding any other binary e.g. java.
PATH is system-reserved variable, that define the way where your system should look to find the executable (in your case java). Therefore you shouldn't use it in your code as variable to your test.jar .
In my opinion, your code should be something like:
#!/bin/sh
PROGPATH='/path/to/your/test.jar'
JAVAEXEC=`which java`
JAVAPARAMS='-j'
GLOBALPATH="$JAVAEXEC $JAVAPARAMS $PROGPATH"
echo $GLOBALPATH