How to execute a java file by shell script - java

How to execute a java file by shell script?

If it's packed up as a jar:
java -jar jarfile.jar

If it is a class file:
java myClassFile
If it is a jar file:
java -jar myJarFile.jar
See:
Tutorial: http://javabeanz.wordpress.com/2009/01/29/running-an-executable-jar-from-command-line/
Official documentation: http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/java.html

java com.foo.Boo

All the answers only show the terminal commands to compile and run .java files but not the shell script to do so.
Below is the shell script. Let's call it compileRunJava.sh
javac $1
java ${1%.java}
You may need to giv terminal the permission to run your script -
$ sudo chmod 754 compileRunJava.sh
Let's say your .java file is Hello.java. To run the script, cd to the directory where you have Hello.java. Run the below command -
$ /path/to/shell/script/directory/compileRunJava.sh Hello.java

There's a nifty little trick after if you're using java 11 or more. To execute a single class you can do the following.
PS: This is for unix based systems, IDK about windows.
Step 1: Create A File by executing the command
$ touch hello
Step 2: Add code to the file by using vi or any other text editor. My code looks like this.
#!/usr/bin/java --source <GET_YOUR_JAVA_VERSION>
public class Main{
public static void main(String... args){
System.out.println("Hello");
}
}
Notice the shebang line. That is required.
Step 3: Make that file executable by executing the following command.
$ chmod +x hello
Step 4: Execute the file
$ ./hello
Hello
This is a great thing that is introduced after java 11. You can read about it more over here.

Related

How to launch java executives from bash directly

If I want to launch one java executable java --jar sample.jar from command line, how could I write the bash script? The following doesn' work
#!/usr/bin/java
--jar $HOME/tools/sample.jar
#!/bin/sh
java --jar $HOME/tools/sample.jar
Java is NOT an interpreter and only a script interpreter can be used in the shebang(#!)
To be complete you cant pass parameters to the interpreter that way anyway..
The "correct" but still wrong way would have been
#!/usr/bin/java --jar
$HOME/tools/sample.jar
I am not fully clear, but Wikipedia hints that the second method might just work..
However, it is up to the interpreter to ignore the shebang line; thus, a script consisting of the following two lines simply echos both lines to standard output when run:
#!/bin/cat
Hello world!
#!/bin/bash
exec java -jar /path/to/jar/the-file.jar "$#"
With $# you pass the bash arguments to jar file

Java re run jar file

I wanna re-run a jar file from its own (with some additional parameters). How can I do this?
I need the solution to be OS independent.
If I deciphered the question correctly, we are talking about command line interface arguments. For this there's plenty tutorials: http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
The code is simple, like you said yourself:
if ("-server".equalsIgnoreCase(argv)) {
// we are server
} else if ("-client".equalsIgnoreCase(argv)) {
// we are client
}
Now, depending on how you want to execute you program from the OS, there's a number of ways:
$java -jar yourjar.jar -client
Or
$java -cp yourjar.jar com.your.program.Main -client
Same for the "-server".
To run them together, either run them from separate terminal windows (or cmd prompts). Or - if in Linux - you can use ampersand:
$java -jar yourjar.jar -client &
$java -jar yourjar.jar -server &

ArrayIndexOutOfBounds Exception on executing linux sh file

I have a program in java which takes 0'th aargument as file location like
File f = new File(args[0]);
so when i execute it using a windows batch(.bat) file it works correctly .
but when i execute the same using a linux shell file(.sh) in linux i get ArrayIndexOutOfBoundsException.
WINDOWS BATCH FILE :
#echo off
for /f %%i in ("%0") do set scriptpath=%%~dpi
set cp=%scriptpath%/../lib/*.jar;
java -classpath %cp% com.synchronizer.main.MYSynchronizer %scriptpath% "%1" "%2"
LINUX SH FILE:
export JAVA_HOME=/usr/local/java
PATH=/usr/local/java/bin:${PATH}
THE_CLASSPATH=
for i in `ls ../lib/*.jar`
do
THE_CLASSPATH=${THE_CLASSPATH}:${i}
done
java -cp ".:${THE_CLASSPATH}" \
com.synchronizer.main.MYSynchronizer
please help!
It looks like a problem in script (no arguments are passed to the Java program).
You can consider to debug the script like this: debugging scripts
Hope this helps
Your shell script is not passing any parameters:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer
Try:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer "$1" "$2"
As stated above, your Linux shell script is not sending any arguments to the Java program that you are trying to start.
And, adding to that, you are not showing us how you run the Linux shell script. If no argument is given on the command line when you start the shell script, no arguments can be passed to your Java application from the shell script.
If you want to see the actual command that is going to be run by your shell script, you can always put "echo" in front of a line and see what all variables are expanded to. This is a simple way to debug shell scripts.

Creating a shell script to run Java program

I used a shell script to run a Java class.
My script contains
#!/bin/sh
java -jar jobs/job.jar
These are my failed attempts to run it.
[root#]#sh testapp.sh
Unable to access jarfile jobs/job.jar
if I just do this at the command line it works fine
[root#]#java -jar jobs/job.jar
thanks.
The best way is to get the current dirname and get in there with this:
#!/bin/sh
cd `dirname "$0"`
java -jar ./job/job.jar
Use the absolute path to your JAR file, e. g. /root/jobs/job.jar.

how to launch java GUI test from linux

-java -classpath<> <classname> in the ".bat" file to launch java test from cmd windows
how to do that using perl to launch java test from linux ?
Don't use perl. For such a simple job, a simple shell script will do:
#!/bin/sh
/path/to/java -classpath foo.jar:bar.jar:. classname
Make the file executable with chmod +x filename and execute it with ./filename
A similar approach using the -jar option is possible. Additionally, you can forward any command line parameters using the special parameter #.
#!/bin/sh
/path/to/java -jar foo.jar "${#}"

Categories