From mboxrd@z Thu Jan 1 00:00:00 1970 From: Torsten Wagner Subject: Re: How-to evaluate Java-snippets in org-mode/org-babel? Date: Mon, 18 Jul 2011 10:31:24 +0900 Message-ID: References: <4E12B6DB.4090001@gmail.com> <87tyb1jdtk.fsf@pinto.chemeng.ucl.ac.uk> <4E145506.70802@gmail.com> <87box75xx6.fsf@gmail.com> <4E1E81AF.4080609@gmail.com> <874o2nlfln.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=0016e68deb7a16616f04a84df5c7 Return-path: Received: from eggs.gnu.org ([140.186.70.92]:50386) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QicgF-0007v2-KI for emacs-orgmode@gnu.org; Sun, 17 Jul 2011 21:31:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QicgC-00064f-Ei for emacs-orgmode@gnu.org; Sun, 17 Jul 2011 21:31:31 -0400 Received: from mail-gy0-f169.google.com ([209.85.160.169]:43456) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QicgB-00064F-Mn for emacs-orgmode@gnu.org; Sun, 17 Jul 2011 21:31:28 -0400 Received: by gyg13 with SMTP id 13so1253629gyg.0 for ; Sun, 17 Jul 2011 18:31:25 -0700 (PDT) In-Reply-To: <874o2nlfln.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Eric Schulte Cc: Org Mode Mailing List --0016e68deb7a16616f04a84df5c7 Content-Type: text/plain; charset=ISO-8859-1 Sorry took me a while to test it. It works great !!!! Many many thanks for your help. I found two minor things. My snippets contain the definition of a package, which in turn end to be a folder. The tangle function could create folders on demand. Would be useful for your code too. It works already by creating the folder manually and simply write... #+begin_src java classname packagename/classname in the next step, people might use sessions as equivalent to a package. This would allow to define multiple classes and the usage of them within a single execution. However for now its perfect already. Why not putting it into ob-java.el and see how its develope. Totti On Jul 16, 2011 12:37 AM, "Eric Schulte" wrote: > Hi Torsten, > > I've just written the included emacs-lisp function [1], which when added > to your config should provide for minimal evaluation functionality for > java code blocks. > > This function drops the body of a code block to a file named after the > value of the :classname header argument (since java cares about file > names), it then compiles the file with javac, and executes the resulting > executable returning the results. See the attached Org-mode file [2] > which evaluates successfully on my system (after I installed javac). > > I copied the code example and compilation process from [3]. > > Please let me know how this works for you, hopefully once we iron out > the kinks in this function it can serve as the seed for a full java > mode. > > Cheers -- Eric > > Torsten Wagner writes: > >> Hi Eric, >> >>> You probably don't want to pass the body of a code block to a lisp >>> function as quoting will become a nightmare, however passing the name to >>> a lisp block may be reasonable. >>> >>> I would suggest that rather than implementing this behavior in a code >>> block you take a look at starting a ob-java.el file. A partial >>> implementation (e.g., only external evaluation, no session evaluation) >>> would be a useful contribution, and given the many helper functions and >>> examples present in the other ob-* files this would most likely be >>> easier than a custom lisp-block implementation. >> >> o.k. the first round of evaluations is over and it worked out >> o.k. However, there was still rather much handwork to do. >> I tried to get a ob-java.el file together using the template and >> mainly by looking at ob-c.el which I guess comes close what would have >> to be done for java. >> However, my lisp-skills (which are close to zero) are not enough to >> get it working. The main problem was that ob-c.el is working for both >> C and C++ and all this if then else troubles me a bit. >> >> Basically, I want only tangle the actual code block into a temp >> file. Well temp is not really right, since java demand its file >> structure and file naming. Finally execute it externally by your >> proposed code >> >> javac -cp . mypackage/myclass.java >> java -cp . mypackage.myclass >> >> and return the results >> >> Hmm maybe better to give a real world example (stripped down to make >> it shorter) >> I use now the following way >> /-----------------------------------------------/ >> #+BABEL: :mkdirp t >> >> * Coursework 1 >> ** StudentID1 >> #+BEGIN_SRC java >> package foo; >> public class Bar >> { >> private double ans = 0 >> public void set(double d){ >> ans = d; >> } >> public void print(){ >> System.out.println(ans); >> } >> public static void main(String[] argv){ >> Bar c = new Bar(); >> c.set(100); >> c.print(); >> } >> } >> #+end_src >> >> ** StudentID2 >> #+BEGIN_SRC java >> package foo; >> public class Bar >> { >> private double x = 0 >> public void set(double in){ >> x = in; >> } >> public void print(){ >> System.out.println(x); >> } >> public static void main(String[] argv){ >> Bar myclass = new Bar(); >> myclass.set(100); >> myclass.print(); >> } >> } >> #+end_src >> >> ** Result >> #+srcname: result >> #+begin_src sh :results output >> javac -cp . foo/Bar.java >> java -cp . foo.Bar >> #+end_src >> >> /----------------------------------------------------/ >> >> >> For now I only added the tangle command to a single code block and >> created the file via C-c C-v t. >> >> #+BEGIN_SRC java tangle: foo/Bar.java >> >> Then I rushed down to a shell block "result" which executed the the >> above commands. I checked the results and started to remove the tangle >> object from one block and added it to the next block. Kind of tiring >> if you have several dozen of blocks. >> Guess you can see from the above example the trouble of having several >> dozen of them and then tangeling them one by one and execute the >> result block ;) >> >> I tried to make it more easy by giving the shell block a name and call >> it under each java code block. This would save me the time going up >> and down in my file. >> >> #+call: result() >> >> However, I noticed that the result update was always done at the first >> appearances of the call , like under the first java code block but not >> at the desired code block?! >> if you fold all together it would look like >> >> /-----------------------------------------------/ >> #+BABEL: :mkdirp t >> >> * Coursework 1 >> ** StudentID1 >> #+BEGIN_SRC java >> #+call: result() >> >> #+results: result >> : 100.0 >> >> ** StudentID2 >> #+BEGIN_SRC java :tangle foo/Bar.java >> #+call: result() >> >> ** Result >> #+srcname: result >> /-----------------------------------------------/ >> >> Calling the second call function updates the result on the first! >> >> Anyhow, I guess having it working with a ob-java.el minimal system >> would be the most easiest. Simply type C-c C-c and it would be done. >> >> Would be very glad if you could help me to get this somehow working. >> >> Totti >> >> > > > Footnotes: > [1] > #+begin_src emacs-lisp > (defun org-babel-execute:java (body params) > (let* ((classname (or (cdr (assoc :classname params)) > (error > "Can't compile a java block without a classname"))) > (src-file (concat classname ".java")) > (full-body (org-babel-expand-body:generic body params)) > (compile > (progn (with-temp-file src-file (insert full-body)) > (org-babel-eval (concat "javac " src-file) "")))) > ((lambda (results) > (org-babel-reassemble-table > (if (member "vector" (cdr (assoc :result-params params))) > (let ((tmp-file (org-babel-temp-file "c-"))) > (with-temp-file tmp-file (insert results)) > (org-babel-import-elisp-from-file tmp-file)) > (org-babel-read results)) > (org-babel-pick-name > (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) > (org-babel-pick-name > (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))) > (org-babel-eval (concat "java " classname) "")))) > #+end_src > > [2] hello-java.org --0016e68deb7a16616f04a84df5c7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

Sorry took me a while to test it.
It works great !!!!
Many many thanks for your help.
I found two minor things.
My snippets contain the definition of a package, which in turn end to be a = folder. The tangle function could create folders on demand. Would be useful= for your code too. It works already by creating the folder manually and si= mply write...

#+begin_src java classname packagename/classname

in the next step, people might use sessions as equivalent to a package. = This would allow to define multiple classes and the usage of them within a = single execution.

However for now its perfect already. Why not putting it into ob-java.el = and see how its develope.
Totti

On Jul 16, 2011 12:37 AM, "Eric Schulte&quo= t; <schulte.eric@gmail.com= > wrote:
> Hi Torsten,
>
> I'= ;ve just written the included emacs-lisp function [1], which when added
> to your config should provide for minimal evaluation functionality for=
> java code blocks.
>
> This function drops the body of= a code block to a file named after the
> value of the :classname hea= der argument (since java cares about file
> names), it then compiles the file with javac, and executes the resulti= ng
> executable returning the results. See the attached Org-mode fil= e [2]
> which evaluates successfully on my system (after I installed = javac).
>
> I copied the code example and compilation process from [3].>
> Please let me know how this works for you, hopefully once w= e iron out
> the kinks in this function it can serve as the seed for = a full java
> mode.
>
> Cheers -- Eric
>
> Torsten Wagner = <torsten.wagner@gmail.com> writes:
>
>> Hi Eric,
>>
>>> You= probably don't want to pass the body of a code block to a lisp
>>> function as quoting will become a nightmare, however passing t= he name to
>>> a lisp block may be reasonable.
>>><= br>>>> I would suggest that rather than implementing this behavior= in a code
>>> block you take a look at starting a ob-java.el file. A partia= l
>>> implementation (e.g., only external evaluation, no sessio= n evaluation)
>>> would be a useful contribution, and given the= many helper functions and
>>> examples present in the other ob-* files this would most likel= y be
>>> easier than a custom lisp-block implementation.
>= ;>
>> o.k. the first round of evaluations is over and it worked= out
>> o.k. However, there was still rather much handwork to do.
>&= gt; I tried to get a ob-java.el file together using the template and
>= ;> mainly by looking at ob-c.el which I guess comes close what would hav= e
>> to be done for java.
>> However, my lisp-skills (which ar= e close to zero) are not enough to
>> get it working. The main pro= blem was that ob-c.el is working for both
>> C and C++ and all thi= s if then else troubles me a bit.
>>
>> Basically, I want only tangle the actual code block in= to a temp
>> file. Well temp is not really right, since java deman= d its file
>> structure and file naming. Finally execute it extern= ally by your
>> proposed code
>>
>> javac -cp . mypackage/myclas= s.java
>> java -cp . mypackage.myclass
>>
>> and= return the results
>>
>> Hmm maybe better to give a real= world example (stripped down to make
>> it shorter)
>> I use now the following way
>> /-= ----------------------------------------------/
>> #+BABEL: :mkdir= p t
>>
>> * Coursework 1
>> ** StudentID1
>> #+BEGIN_SRC java
>> package foo;
>> public c= lass Bar
>> {
>> private double ans =3D 0
= >> public void set(double d){
>> = ans =3D d;
>> }
>> public void print(){
>>= System.out.println(ans);
>> }
>= > public static void main(String[] argv){
>> = Bar c =3D new Bar();
>> c.set(100);
>> c.prin= t();
>> }
>> }
>> #+end_src
>&= gt;
>> ** StudentID2
>> #+BEGIN_SRC java
>> pa= ckage foo;
>> public class Bar
>> {
>> private d= ouble x =3D 0
>> public void set(double in){
>>= x =3D in;
>> }
>> = public void print(){
>> System.out.println(x);
>> }>> public static void main(String[] argv){
>> = Bar myclass =3D new Bar();
>> my= class.set(100);
>> myclass.print();
>> }
>= > }
>> #+end_src
>>
>> ** Result
>>= ; #+srcname: result
>> #+begin_src sh :results output
>> = javac -cp . foo/Bar.java
>> java -cp . foo.Bar
>> #+end_src
>>
>> /= ----------------------------------------------------/
>>
>&g= t;
>> For now I only added the tangle command to a single code blo= ck and
>> created the file via C-c C-v t.
>>
>> #+BEGIN_SR= C java tangle: foo/Bar.java
>>
>> Then I rushed down to a= shell block "result" which executed the the
>> above c= ommands. I checked the results and started to remove the tangle
>> object from one block and added it to the next block. Kind of tiri= ng
>> if you have several dozen of blocks.
>> Guess you c= an see from the above example the trouble of having several
>> doz= en of them and then tangeling them one by one and execute the
>> result block ;)
>>
>> I tried to make it more ea= sy by giving the shell block a name and call
>> it under each java= code block. This would save me the time going up
>> and down in m= y file.
>>
>> #+call: result()
>>
>> However, I no= ticed that the result update was always done at the first
>> appea= rances of the call , like under the first java code block but not
>&g= t; at the desired code block?!
>> if you fold all together it would look like
>>
>>= ; /-----------------------------------------------/
>> #+BABEL: :m= kdirp t
>>
>> * Coursework 1
>> ** StudentID1 >> #+BEGIN_SRC java
>> #+call: result()
>>
>&= gt; #+results: result
>> : 100.0
>>
>> ** Studen= tID2
>> #+BEGIN_SRC java :tangle foo/Bar.java
>> #+call: = result()
>>
>> ** Result
>> #+srcname: result
>> /-= ----------------------------------------------/
>>
>> Cal= ling the second call function updates the result on the first!
>><= br> >> Anyhow, I guess having it working with a ob-java.el minimal system=
>> would be the most easiest. Simply type C-c C-c and it would be= done.
>>
>> Would be very glad if you could help me to g= et this somehow working.
>>
>> Totti
>>
>>
>
>
>= ; Footnotes:
> [1]
> #+begin_src emacs-lisp
> (defun= org-babel-execute:java (body params)
> (let* ((classname (or (cd= r (assoc :classname params))
> (error
> &q= uot;Can't compile a java block without a classname")))
> = (src-file (concat classname ".java"))
> = (full-body (org-babel-expand-body:generic body params))
> (compile
> (progn (with-temp-file src-fil= e (insert full-body))
> (org-babel-eval (concat &q= uot;javac " src-file) ""))))
> ((lambda (results= )
> (org-babel-reassemble-table
> (if (member &qu= ot;vector" (cdr (assoc :result-params params)))
> = (let ((tmp-file (org-babel-temp-file "c-")))
> = (with-temp-file tmp-file (insert results))
> (org-babel-import-elisp-from-file tmp-file))
> = (org-babel-read results))
> (org-babel-pick-name=
> (cdr (assoc :colname-names params)) (cdr (assoc :colnam= es params)))
> (org-babel-pick-name
> (cdr (assoc :rowname= -names params)) (cdr (assoc :rownames params)))))
> (org-babel= -eval (concat "java " classname) ""))))
> #+end_s= rc
>
> [2]
hello-java.org
=
--0016e68deb7a16616f04a84df5c7--