https://github.com/davidmoten/java-script-template
Template for a bash script that compiles and runs java commands
https://github.com/davidmoten/java-script-template
java scripting
Last synced: over 1 year ago
JSON representation
Template for a bash script that compiles and runs java commands
- Host: GitHub
- URL: https://github.com/davidmoten/java-script-template
- Owner: davidmoten
- License: apache-2.0
- Created: 2014-05-07T05:02:47.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2021-07-13T04:38:33.000Z (about 5 years ago)
- Last Synced: 2025-04-10T22:15:34.225Z (over 1 year ago)
- Topics: java, scripting
- Language: Shell
- Size: 14.6 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
java-script-template
====================
Can you write a script with Java?
-----------------------------------
So there are lots of great scripting languages out there that offer a lot over plain old shell scripting. If you know java
then [groovy](http://groovy.codehaus.org/) is very close and a good option but there is the alternative to write pure java by running the source with *runjava* (below) or embedding it into a bash script
like [example.sh](https://github.com/davidmoten/java-script-template/blob/master/example.sh).
How to run java source in a separate file
---------------------------------------------------
If you want to run java source in a separate file then use [runjava](https://github.com/davidmoten/java-script-template/blob/master/runjava). For example:
Example.java:
```java
import java.io.*;
import java.text.*;
import java.util.*;
public class Example {
public static void main(String[] args) throws Exception {
System.out.println("Hello " + args[0] + "!");
}
}
```
Run it like this (passing `dave` as an argument):
```bash
$ ./runjava Example.java dave
Hello dave!
```
On my i7 desktop it takes 0.8 seconds to run (compile and execute). Same startup lag as groovy (which you would expect!).
How to embed java source in a bash script
------------------------------------------
[example.sh](https://github.com/davidmoten/java-script-template/blob/master/example.sh) is a bash script that has Example.java embedded in it.
It is designed to be run with a linux/unix bash shell.
The script is run by entering ./example.sh at the command line.