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}"
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 "$#"
Everytime I want to build and run my program I do:
javac myProgram.java
java myProgram
I want to do something like this:
buildrun = javac (some_argument).java && java (some_argument)
so after I can just
buildrun myProgram
How to achieve this on Windows?
As other's have suggested you can simply create a batch file to build and run your program. Copy this in Notepad and save as .bat.
#echo off
set /p class="Enter Class: "
javac "%class%".java
java "%class%"
As you want, the batch file will ask for a FileName when it runs. In your case, you can set it to 'myProgram' and it will then compile and run the program.
Just make sure your batch file and your java file reside in the same folder for this script to run. You can always tweak the bat file to even accept the full path of the Java file.
To compile and run Java with single command line in cmd use:
java myProgram.java
You can use Makefile this way:
Create a file named Makefile within the same folder where your java files reside.
Add these contents to Makefile
run: compile
java $(class)
compile:
javac $(class).java
From terminal, run this command:
make run class=myProgram
This way, it will compile first then run your java class in a single command
Yet another solution. Create buildrun.cmd file with the following code:
#echo off
javac %1.java
if errorlevel = 0 goto RUN
:ERROR
echo "Build fails!"
goto END
:RUN
java %1
:END
Now you can pass the class name which should be processed. For example: buildrun MyProgram to compile MyProgram.java and run MyProgram.class
In this case execution will performs only if your class was compiled successful.
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 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.
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&