emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Schulte <schulte.eric@gmail.com>
To: Torsten Wagner <torsten.wagner@gmail.com>
Cc: Org Mode Mailing List <emacs-orgmode@gnu.org>
Subject: Re: How-to evaluate Java-snippets in org-mode/org-babel?
Date: Fri, 15 Jul 2011 09:00:12 -0600	[thread overview]
Message-ID: <874o2nlfln.fsf@gmail.com> (raw)
In-Reply-To: 4E1E81AF.4080609@gmail.com

[-- Attachment #1: Type: text/plain, Size: 6373 bytes --]

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 <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 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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: hello-java.org --]
[-- Type: text/x-org, Size: 224 bytes --]


#+begin_src java :classname myfirstjavaprog
  class myfirstjavaprog
  {  
      public static void main(String args[])
      {
          System.out.println("Hello World!");
      }
  }
#+end_src

#+results:
: Hello World!


[-- Attachment #3: Type: text/plain, Size: 105 bytes --]


[3]  http://www.javacoffeebreak.com/java101/java101.html

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

  reply	other threads:[~2011-07-15 15:37 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-05  7:01 How-to evaluate Java-snippets in org-mode/org-babel? Torsten Wagner
2011-07-05  9:07 ` Eric S Fraga
2011-07-05  9:22   ` Eric S Fraga
2011-07-05 13:48   ` Jason F. McBrayer
2011-07-06 12:36     ` Torsten Wagner
2011-07-06 13:26     ` Eric Schulte
2011-07-06 15:14       ` Jason F. McBrayer
2011-07-06 17:11         ` Torsten Wagner
2011-07-06 19:15         ` Eric S Fraga
2011-07-06 12:28   ` Torsten Wagner
2011-07-06 13:38     ` Eric Schulte
2011-07-14  5:42       ` Torsten Wagner
2011-07-15 15:00         ` Eric Schulte [this message]
2011-07-18  1:31           ` Torsten Wagner
2011-07-18 14:53             ` Eric Schulte
2011-07-28 10:49               ` Torsten Wagner
2011-07-29  0:06                 ` Eric Schulte

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874o2nlfln.fsf@gmail.com \
    --to=schulte.eric@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=torsten.wagner@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).