From: SolutionMonkey on 25 Mar 2010 00:08 Hi: Is it possible to compile and execute a bit of java code in one line? I know its possible in dynamic languages like groovy. e.g. groovy -e "println 'Hello World!' " I'm guessing you can't do this in java since there is a separate compilation step, but I thought maybe there's a compile/execute tool or option I'm unaware of. Thanks for your comments.
From: Thomas Pornin on 25 Mar 2010 07:36 According to SolutionMonkey <valuemonkey(a)telus.net>: > I'm guessing you can't do this in java since there is a separate > compilation step, but I thought maybe there's a compile/execute tool > or option I'm unaware of. The Java compiler is itself written in Java, and can be invoked programmatically; since Java 6, the API for that is standard (javax.tools.JavaCompiler). Also, Java supports dynamic loading of classes, so what you look for is conceptually doable. It can be done entirely in RAM without using files; see for instance: http://www.java2s.com/Code/Java/JDK-6/CompilingfromMemory.htm However, Java is not very appropriate for one-liners; the package, imports, class declaration, method prototypes... are somewhat huge and cumbersome when using a single command-line invocation. You may want to add some pre-processing. Also, note that : -- This requires a JVM with the Java compiler, i.e. a JDK. A mere JRE (what most people have, to run applets and applications) does not contain the Java compiler. -- The kick-start of a JVM and a compilation pass tend to be computationally expensive. I have tried; expect some delay (e.g. one second). For usual Java tasks this is not a problem, but for a command-line interface I expect something much more responsive. Such delays quickly become irksome. --Thomas Pornin
From: bugbear on 25 Mar 2010 07:49 SolutionMonkey wrote: > Hi: > > Is it possible to compile and execute a bit of java code in one line? > I know its possible in dynamic languages like groovy. > > e.g. groovy -e "println 'Hello World!' " > > I'm guessing you can't do this in java since there is a separate > compilation step, but I thought maybe there's a compile/execute tool > or option I'm unaware of. Well, one *could* create a script to invoke the compiler and runtime components sequentially, to give the effect you request, but I see little benefit. BugBear
From: Alessio Stalla on 25 Mar 2010 07:52 On Mar 25, 5:08 am, SolutionMonkey <valuemon...(a)telus.net> wrote: > Hi: > > Is it possible to compile and execute a bit of java code in one line? > I know its possible in dynamic languages like groovy. > > e.g. groovy -e "println 'Hello World!' " > > I'm guessing you can't do this in java since there is a separate > compilation step, but I thought maybe there's a compile/execute tool > or option I'm unaware of. > > Thanks for your comments. BeanShell is a Java interpreter written in Java. I have never used it, but it might do what you ask.
From: bugbear on 25 Mar 2010 09:47
Stefan Ram wrote: > bugbear <bugbear(a)trim_papermule.co.uk_trim> writes: >> Well, one *could* create a script to invoke the compiler >> and runtime components sequentially, to give the effect >> you request, but I see little benefit. > > One benefit would be the possibility to use Java package > in command lines, such as Perl is used in, say, > > cat cljp | perl -pe 's/\n\nFrom/\nFrom /mg' | grep ... Yes - I understand the "idea" but question wether Java can (in practice) express anything very useful in one line. BugBear |