LoginSignup
6
6

More than 5 years have passed since last update.

コンパイルしないJava

Last updated at Posted at 2015-07-08

Cをオンザフライでコンパイルして実行するやつJava版

これをj1という名前で保存する。

#!/usr/bin/env bash

temp=$(mktemp /tmp/JXXXXXX)
class=$(basename $temp)
outdir=$(dirname $temp)

{
  echo 'public class '$class'{'
  echo 'public static void main(String[] args){'
  echo $@
  echo ';}}'
} >> $temp.java

#cat $temp.java

{
  javac $temp.java
  java -cp $outdir $class < /dev/stdin
}

こうなるけど、C以上にうれしくない。

~/s/c ❯❯❯ ./j1 'System.out.println("Hello world.")'
Hello world.

普通の用途にはJava REPLとか、Nashorn,JRuby,Groovy,Clojureあたりをおすすめします。

6
6
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6