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"
Related
I have to write two scripts, one to compile my code and another one to run it. I manage to compile the code with this script:
#!/bin/bash
javac SimilaridadeP.java
And I was able to run it using this script:
#!/bin/bash
java SimilaridadeP
The problem is that I need to execute my second shell script following this command:
./execute.sh input output
Where input and output are the names of the files that I am going to use in my Java code. The problem that I am facing right now is that I don't know how I can use these both names in my Java code.
So far I managed to get both names using this code:
#!/bin/bash
in=$1
out=$2
java SimilaridadeP
But since "SimilaridadeP" is the name of my java file and it can't be called like a method with parameters I am kind of lost.
If your java code is already reading the command-line parameters, it's just a matter of changing the bash this way:
#!/bin/bash
java SimilaridadeP "$1" "$2"
This works with arbitrary number of arguments:
#!/bin/bash
java SimilaridadeP "$#"
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.
I have created a jar file , it reads from standard input and print them out to the standard output.
Below is how you can use the app:
cat /ephemeral/test/input | java -jar /ephemeral/test/Hello.jar >> /ephemeral/test/output
I am wondering is it possible to add my Hello.jar to be a Linux Command, let's say -hello-:
So next time I could just do something like:
hello /ephemeral/test/input /ephemeral/test/output
I am not that much familiar with Linux Soft Link and Java, so any help or information would be appricated!
UPDATE:
thanks for reno's inspiring answer, I put the cat ... | .. >> .. into a shell script hello.sh.
Now I can do something like:
./hello.sh /inputpath/input outputpath/output
(Note: you should not use $0, $0 is actually the name the file itself instead of input arguments)
Now, the last question is how could you access the shell script EVERYWHERE, like how you call basic shell cmds: ls..cat..echo..
I have read a tutorial described exactly what I want(reach to the end of the tutorial directly):
I did add the path of the directory where the hello.sh exists into the path variable.
I created a bin folder which is like mkdir /home/ubuntu/bin and put my shell script there.
However, I still have problem do what the author described:
hello input output (in my case)
Does anyone know what is going wrong here?
you can write a shell script say test.sh and copy your code to test.sh
!/bin/bash
cat $0 | java -jar /ephemeral/test/Hello.jar >> $1
and execute the script by passing your input and output file name something like this:
test.sh /ephemeral/test/input /ephemeral/test/output
The way to do this is to write a shell script (usually hello.sh) that contains the necessary java -jar command.
I have a Perl file which is actually calling some jar files and I'm trying to make it an all Java program and remove the Perl. So I came up through this lines which is:
$blammercommand="$javapath $javaparams -jar $blammerpath -conf $blammerconf $blammerparams -cpu $cpu -i \"".$tmpdir."blastresults/*.bls\" -db \"$infilename $blastdb\"";
(system($blammercommand)==0) or die "unable to do $blammercommand\n";
I've already decompressed the jar files and added the source codes to my Eclipse project and have access to the main function of the related jar file. I'm just trying to pass the arguments as inputs.
My problem is exactly here that I don't know what "\"".$tmpdir."blastresults/*.bls\"" and "\"$infilename $blastdb\"" mean. I know what exactly each one of this variables are but I don't know how those \, / and * are working and how should I convert them to Java.
Those are just shell escaping and shell globbing. Literally written, it would look like this:
${javapath} ${javaparams} -jar ${blammerpath} -conf ${blammerconf} ${blammerparams} -cpu ${cpu} -i "${tmpdir}blastresults/*.bls" -db "${infilename} ${blastdb}
Here, the syntax ${name} is meant to indicated an inserted value of the variable. The system command in Perl runs a system command through the default system shell, usually something like bash. The quotes are used to make several space-separated strings "stick together" as one argument. The * is a wildcard, which is replaced by all filenames in a given directory.
I have an application that reads a text file.
If the application reads it from stdin, then I could forward the input / pipe it
$ app < input.txt
$ cat input.txt | app
Unfortunately, the application expects a filename as an argument..
$ app --input input.txt
and I can't easily change the source of input (eg. swap local file [as in example] for a result of a wget).
Is there an option to somehow trick this app?
If a solution requires a temporary file, it is necessary that this file is not left on the FS..
The app is actually an executable JAR; I'd like the trick to run on Win/Lin..
I am not sure if it could work for you, but I have already solve a similar problem with a fifo.
http://linux.die.net/man/3/mkfifo
This could give you the indirection between your app and your source.
mkfifo fifo.input
app --input fifo.input
Now you can fill the pipe with different sources.
cat input > fifo.input
ctrl-c
cat otherInput > fifo.unput
Hope it solve your problem on Linux. So far I know it does not exist on Windows.
You want to do something like this.
java -jar myjar.jar arg1 arg2
where myjar.jar is the executable jar and arg1 arg2 etc are parameters the program expects.
So suppose you want to pass input.txt to your application and its the only commandline parameter required then command will be
java -jar myjar.jar input.txt