开发者

How can i run my statements from the terminal for Java?

开发者 https://www.devze.com 2023-04-06 17:25 出处:网络
How can i run my statements from the terminal? $ java -c Sy开发者_高级运维stem.out.println(\"test\");

How can i run my statements from the terminal?

$ java -c Sy开发者_高级运维stem.out.println("test");
bash: syntax error near unexpected token `('


Look up BeanShell! It's not quite "pure" Java, but it's super close and really useful! www.beanshell.org


What you are looking for is Groovy, it is a dynamic superset of Java. Which means you can run any Java statement as a Groovy statement. It has an interactive REPL shell that can execute Java statements or Groovy statements equally as well.

You can also write scripts using plain Java statements as well, no need for Groovy syntax short cuts or extensions and run them as scripts.

groovy -e "println 'Hello ' + args[0]" World


If you don't need something professional, you can use simple shell script to wrap your statement in function and class body, compile and execute it.

#!/bin/sh

echo "class Main\n{\npublic static void main(String[] args)\n{" > /tmp/Main.java
echo $* >> /tmp/Main.java
echo "\n}\n}\n" >> /tmp/Main.java

javac /tmp/Main.java || exit 1
exec java -cp /tmp/ Main
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号