Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a java file by shell script

Tags:

java

shell

How to execute a java file by shell script?

like image 262
BOSS Avatar asked Mar 31 '11 09:03

BOSS


4 Answers

If it's packed up as a jar:

java -jar jarfile.jar
like image 89
das_weezul Avatar answered Sep 29 '22 08:09

das_weezul


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
like image 23
ZeWaren Avatar answered Sep 29 '22 09:09

ZeWaren


java com.foo.Boo

like image 25
adarshr Avatar answered Sep 29 '22 09:09

adarshr


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
like image 29
user11418600 Avatar answered Sep 29 '22 09:09

user11418600