I have a java class, cs.class, that I would like to execute from the command line, just as you use any other command. I would like to be able to type 'cs file1' etc. Without having to use 'java cs file1'. How can I do this?
edit: I would also like this to work if I put my class anywhere in my path.
It looks like this will work for you on linux.
For Windows
Create a file named custom.cmd and add the following to it
#echo off
DOSKEY cs=java cs $*
Now
right click your command prompt shortcut->properties->shortcut tab->and append the following to your target field
/K C:\custom.cmd
my custom.cmd resides in C:, change the path to yours
Now you can use "cs" as a command within that cmd shell. You can mention you filename as an argument as well as $* specifies command line arguments.
Related
I am trying to create to create a script that will automatically change the working directory to my Java code and then compile the code. I am able to change the directly to my path but I cannot figure out how to to call the java compiler on the "filename" while taking the "filename" as an argument directly from the Terminal.
#!/bin/bash
FILE = “$1”
open -a Terminal /Users/Jarvis/Desktop/Codes/Java/CS\ 49J/
javac “$FILE”
Remove the spaces in your FILE assignment. Also, as #cricket_007 mentions, you don't need to open the terminal app, just change directory.
#!/bin/bash
FILE="$1"
cd /Users/Jarvis/Desktop/Codes/Java/CS\ 49J/
javac "$FILE"
Alternatively (I'm in the habit of using curly braces around variables):
#!/bin/bash
FILE="$1"
WORKING_DIR="/Users/Jarvis/Desktop/Codes/Java/CS\ 49J/"
javac "${WORKING_DIR}${FILE}"
I'm trying to write a batch file (run.bat) that can be invoked something like this:
run.bat "whatever.log"
But under the hood, the batch file is passing the argument ("whatever.log") to the following command:
java -cp "fizz.jar;a.jar;b.jar;c.jar;d.jar" com.myapp.FizzDriver "whatever.log"
Again, if you run: run.bat "blah.txt", then that batch file would execute:
java -cp "fizz.jar;a.jar;b.jar;c.jar;d.jar" com.myapp.FizzDriver "blah.txt"
My best attempt at run.bat so far is:
#ECHO OFF
%JAVA_HOME%\bin\java java -cp "fizz.jar;a.jar;b.jar;c.jar;d.jar" com.myapp.FizzDriver ???
But I'm not sure how to parameterize the argument (???). I'm also not sure if the batch file is missing anything or is incorrect with the way I've written it. Ideas? Thanks in advance!
You just need to put %1, but you have another issue. When you use 'java -jar', the '-cp' argument is ignored: the CLASSPATH is taken from the jar manifest only.
To pass one command line parameter, use %1. If you need to pass more than one, you can either use %1 %2 etc, or use %* to pass all of them at once.
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 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
In a bash shell script I tried these two versions:
java -jar abc.jar&
and
CMD="java -jar abc.jar&"
$CMD
The first verison works, and the second version complains that abc.jar cannot be found. Why?
Commands do run from current directory in a shell script.
This is why the first command in your test script worked.
The second command may not work because either java isn't in your ${PATH} or abc.jar isn't in your ${CLASSPATH}. You could echo these environment variables or set +x to debug your bash script.
Bash (and others) won't let you do backgrounding (&) within the value of a variable (nor will they let you do redirection that way or pipelines). You should avoid putting commands into variables. See BashFAQ/050 for some additional information.
What is the actual error message you're getting? I bet it's something like "abc.jar& not found" (note the ampersand) because the ampersand is seen as a character in the filename.
Also, the current directory for the script is the directory that it is run from - not the directory in which it resides. You should be explicit about the directory that you want to have your file in.
java -jar /path/to/abc.jar&